-3

Can't seem to fix my code, whenever i try to input a value, it appears as an error. Also, i used the ibio thing to obtain input so please disregard that when making code corrections and thanks by the way. it would help me a lot since i can't seem to figure out how to do this...

edit: i ran into another problem and the user input always becomes scissors, despite what they enter. for example:

Options: Rock, Paper, Scissors
Your choice? Rock
You have selected s

 public class rockPaperScissors
 {
    public static void main (String args[])
    {
        new rockPaperScissors ();
    }

     public rockPaperScissors ()
    {
        int cPoint = 0;
        int uPoint = 0;
        int game = 1;
        char playAgain = 'y';
        System.out.println ("-----Rock, Paper, Scissors------\n");
        while (playAgain == 'y')
    {
            char user = userChoice ();
            System.out.println ("You have selected " + user);
            char comp = compChoice ();
            System.out.println ("The computer has selected " + comp);
            char win = winner (comp, user);
            if (win == 'c')
            {
                cPoint++;
                System.out.println ("\nThe computer wins!");
            }
            else if (win == 'u')
            {
                uPoint++;
                System.out.println ("\nYou win!");
            }
            else
                System.out.println ("\nThere is a tie!");
            System.out.println ("Points: You: " + uPoint + " Computer: " +     cPoint);
            playAgain = IBIO.inputChar ("\nPlay again? (y/n) ");
            System.out.println ("");
        }
        System.out.println ("Goodbye!");
    }


    public boolean isValid (String c)
    {
        /* All valid data:
               Rock, ROCK, rock, r, R
               Paper, PAPER, paper, p, P
               Scissors, SCISSORS, scissors, s, S
           return true if valid, false otherwise
        */
        if (c=="Rock" || c=="ROCK" || c == "rock" || c=="r" || c=="R" ||     c=="Scissors" || c=="SCISSORS" || c=="scissors" || c=="s" || c=="S" || c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
        return true;
        else
        return false;
    }


    public char userChoice ()
    { //returns r, p or s, based on the user's choice

        //print options: rock, paper, scissors
        System.out.println ("Options: Rock, Paper, Scissors");
        //ask for user's choice. will need to store in String
        String c = IBIO.inputString ("Your choice? ");
        //Loop: if invalid input, ask again
        //stopping condition is the isValid method!!
        //something like: while(!isValid(choice))
        while (!isValid(c))
         {
          System.out.println ("That choice is invalid. Try another choice.");
          c = IBIO.inputString ("Your choice? ");
         }

        //If: to standardize values
        //if you've got one of Rock, ROCK, rock, r, R, then return 'r'.
        if (c=="Rock" || c=="ROCK" || c=="rock" || c=="r" || c=="R")
           return 'r';
        //else if you've got one of Paper, PAPER, paper, p, P, then return 'p'.
        else if (c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
           return 'p';
        //else return 's';
        else
           return 's';

    }


    public char compChoice ()
    { //make a random number between 1 and 3
         //if the number is 1, return r; 2 return s; 3 return p
    int num = (int)(Math.random () * 3) + 1;
    if (num == 1)
        return 'r';
    else if (num == 2)
        return 's';
    else
        return 'p';
}


    public char winner (char comp, char user)
    { //comp and user both hold one of r, s, p
        //returns c for computer, u for user and b for both
    if ((comp == 'r' && user == 's') || (comp == 's' && user == 'r') || (comp == 'p' && user == 's'))
        return 'c';
    if ((user == 'r' && comp == 's') || (user == 's' && comp == 'r') || (user == 'p' && comp == 's')) 
        return 'u';
    else
        return 'b';
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • What is the text of your error? Just saying you got "an error" isn't specific enough. – kunruh Mar 18 '16 at 19:05
  • my code works, but no matter what i type my output does not go through even if it is valid. i programmed it to say "That choice is invalid. Try another choice." but it says that even for valid choices – BadAtComSci Mar 18 '16 at 19:09
  • your program is doing exactly what it is written to do.. Just because you programmed it, and you "think" it should do something, does NOT mean that it is going to do what you "think" it will. – austin wernli Mar 18 '16 at 19:12
  • 1
    Just look at the duplicate... Read it carefully... then look at your code at `c=="Rock"`, for example – OneCricketeer Mar 18 '16 at 19:13
  • so can you help me fix the user input to be accurate since it always defaults to scissors? i'm not sure what to do – BadAtComSci Mar 18 '16 at 19:16
  • 1
    Please change all the times you do *any variable* `==` "something in double quotes" to use `.equals` then come back and tell us if it works or not – OneCricketeer Mar 18 '16 at 19:22
  • 1
    *"i'm not sure what to do"* As cricket already told: *"Just look at the duplicate... Read it carefully"*. – Tom Mar 18 '16 at 19:23
  • thanks a lot cricket_007 it works now :))) – BadAtComSci Mar 18 '16 at 19:31

1 Answers1

1

Your problem is obvious that everytime you compare a String using ==, it returns false. I told you to look at the duplicate to fix that, but I highly suggest breaking your code into smaller sections with some methods.

  public boolean isRock(String input) {
      String lower = input.toLowerCase();
      return lower.equals("r") || lower.equals("rock");
  }

  public boolean isPaper(String input) {
      String lower = input.toLowerCase();
      return lower.equals("p") || lower.equals("paper");
  }

  public boolean isScissors(String input) {
      String lower = input.toLowerCase();
      return lower.equals("s") || lower.equals("scissors");
  }

  public boolean isValid(String input) {
      return isRock(input) || isPaper(input) || isScissors(input);
  } 

That will simplify your other conditions into

if (isRock(c))
   return 'r';
else if (isPaper(c))
   return 'p';
else
   return 's';
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245