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';
}
}