Results 1 to 5 of 5

Thread: I soo need help

  1. #1
    beauti477 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Angry I soo need help

    I am trying to write the code for the Rock paper Scissors game and I am stuck, I think I am about 75% there, except I keep getting a message that:

    RockPaperScissors.java:58: variable cChoice might not have been initialized
    return cChoice;

    I am trying not to have to set this to a value like 'X', cause when I do it comes out in my random answers.

    Choose: [(R)Rock,(P)Paper,(S)Scissors,(Q)Quit]: R
    Computer:P, Human:R,
    Choose: [(R)Rock,(P)Paper,(S)Scissors,(Q)Quit]: P
    Computer:P, Human:P,
    Choose: [(R)Rock,(P)Paper,(S)Scissors,(Q)Quit]: P
    Computer:X, Human:P,

    Any ideas how I get around this?

    Java Code:
    import java.io.*; 
      
    public class RockPaperScissors
    {
      static char userChoice() throws Exception
      {
          // declare variables
          char uChoice; // the default value
    
          BufferedReader cin;
          cin = new BufferedReader(new InputStreamReader(System.in));
    
          //read users choice
         while (true)
         {
          System.out.print("Choose: [(R)Rock,(P)Paper,(S)Scissors,(Q)Quit]: ");
          uChoice = cin.readLine().charAt(0);
          if 
          (uChoice == 'R') break;
          else if
          (uChoice == 'P') break;
          else if
          (uChoice == 'S') break;
          else
            System.out.print("Thank you for playing!");
            System.out.println();
            break;
          }
          return uChoice;
      }
      static char compChoice() throws Exception
        {
          // declare variables
          int cOmputer;
          char cChoice = 'X';  // the default value
    
          BufferedReader cin;
          cin = new BufferedReader(new InputStreamReader(System.in));
    
          //read computers choice
          while (true)
          {
            cOmputer = (int)(3 * Math.random());
         
            if (cOmputer == 1)
            cChoice = 'R';
            else if (cOmputer == 2)
            cChoice = 'P';
            else if (cOmputer == 3)
            cChoice = 'S'; 
    
            return cChoice;
          }
        }
    
        public static void main(String[] argv) throws Exception
        {
          char pChoice;
          char cChoice;
    
          while (true)
          {
          pChoice = userChoice();
          cChoice = compChoice();
          if (pChoice == 'Q') break;
          if (pChoice == 'R' || pChoice == 'P' || pChoice == 'S')
          System.out.print("Computer:" + cChoice + ", Human:" + pChoice + ",");
          System.out.println();
          }
        }
    }
    Last edited by beauti477; 07-20-2011 at 09:08 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Could you edit your code to make it readable? Put [code] at the start of the code and [/code] at the end. You may have to replace the indentation as well.

  3. #3
    beauti477 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    sorry, hope that is better

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Thanks.

    So that method is returning an X. That's strange on the face of it because you say "cOmputer = (int)(3 * Math.random());". So shouldn't it return R, P or S? Time to debug:

    Java Code:
    static char compChoice() throws Exception
      {
        // declare variables
        int cOmputer;
        char cChoice = 'X';  // the default value
    
        //read computer's choice
        while (true)
        {
          cOmputer = (int)(3 * Math.random());
          [color=green]System.out.println("Choice for computer is " + cOmputer);[/color]
         
          if (cOmputer == 1)
            cChoice = 'R';
          else if (cOmputer == 2)
            cChoice = 'P';
          else if (cOmputer == 3)
            cChoice = 'S'; 
    
          return cChoice;
        }
      }
    I've removed the BufferedReader because you aren't using it! (Also I have no clue why you say while(true)...).

    Run the program lots of times and make sure you get the numbers you expect: 1, 2 or 3 each equally likely.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    (int)(3 * Math.random()) returns a value 0, 1 or 2 (check this).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •