Just some simple Java question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Niner49
    New Member
    • Oct 2007
    • 7

    Just some simple Java question

    I want my program to ask the user if they would like to play another game of rock, paper, scissors. However, I want it to only accept (y for yes or n for no). At this point all I have been able to do is create a infinite loop, or just make the program not compile altogether. I have converted the char to a string, and attempted to use the .equals method but no such look.

    I know it's a really simple question, but any suggestion would appreciated
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Post your best attempt (just the user prompting, not the rock-paper-scissors.

    Comment

    • Niner49
      New Member
      • Oct 2007
      • 7

      #3
      Originally posted by BigDaddyLH
      Post your best attempt (just the user prompting, not the rock-paper-scissors.
      [CODE=java]//Ask the user if he/she would like to play another game
      System.out.prin t("\n\nWould you like to play another game ('y' for yes, 'n' for no): ");
      cntrl = kb.next().charA t(0);
      con = new Character(cntrl ).toString();

      //A if statement that determines the whether the user wants to play another game
      while(con.equal s('y') || con.equals('Y') || con.equals('n') || con.equals('N') != true)
      {
      System.out.prin t("Sorry, you must enter y for yes, or n for no: ");
      cntrl = kb.next().charA t(0);
      }

      if(con.equals(' y') || con.equals('Y') )
      {
      ansr = true;
      comTalley = 0;
      useTalley = 0;
      rslt = 0;
      roundNum = 1;
      gameNum++;
      System.out.prin tln("\n\n\n\n\n ");
      }

      else if(con.equals(' n') || con.equals('N') )
      {
      ansr = false;
      }[/CODE]


      I had it working when the program didn't care what you entered to end, but I just want it to accept 'n' to end the loop that i have it in.
      Last edited by Ganon11; Feb 22 '08, 02:57 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        [CODE=Java] cntrl = kb.next().charA t(0);[/CODE]

        1. what if the user just hits return? I suspect kb.next() would return "" and so charAt(0) would be out-of-bounds!

        2. You are confusing String and char. Stick with chars, which are simpler and because they are a primitive type, should be compared with == and !=

        [CODE=Java]if (cntrl == 'y') {
        ...
        if (cntrl != 'n') {
        [/CODE]

        Comment

        • Niner49
          New Member
          • Oct 2007
          • 7

          #5
          Originally posted by BigDaddyLH
          [CODE=Java] cntrl = kb.next().charA t(0);[/CODE]

          1. what if the user just hits return? I suspect kb.next() would return "" and so charAt(0) would be out-of-bounds!

          2. You are confusing String and char. Stick with chars, which are simpler and because they are a primitive type, should be compared with == and !=

          [CODE=Java]if (cntrl == 'y') {
          ...
          if (cntrl != 'n') {
          [/CODE]

          I'll give it a try, thanks for the advice. Sometimes I have problems with over complicating the simplest of things

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Niner49
            I'll give it a try, thanks for the advice. Sometimes I have problems with over complicating the simplest of things
            There is a golden rule called KISS.

            Comment

            Working...