i'm really not sure what's wrong with my code. It's supposed to do rock paper scissors against the computer by taking in the user choice, comparing it to the random computer choice, and displaying the results.
I get two errors that i have no return statements for the 3rd and 4th methods. Also, when i run it without fixing the errors, the nested if statements starting at line 60 only print out one of the two println statements, which really makes zero sense to me.
import java.util.Random;
import java.util.Scanner;
public class Chapter5ProjectPart2 {
public static void main(String[] args) {
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
int userNum;
int compNum;
String userChoice = "";
String compChoice = "";
int rnum;
int result = 0;
boolean keepPlaying;
int input = 1;
do
{
compNum = generator.nextInt(2)+1;
compChoice = numToChoice(compNum);
menu();
userNum = keyboard.nextInt();
userChoice = numToChoice(userNum);
keyboard.nextInt();
System.out.println();
System.out.println("you chose " + userChoice);
System.out.println("the computer chose " + compChoice);
result = resultCheck(userNum, compNum);
if (result == 1) // user wins
{
if (userNum == 1) //user won choosing rock
{
System.out.println("rock beats scissors");
System.out.println("you win");
}
else if (userNum == 2) //user won choosing paper
{
System.out.println("paper beats rock");
System.out.println("you win");
}
else if (userNum == 3) //user won choosing scissors
{
System.out.println("scissors beats paper");
System.out.println("you win");
}
}
else if (result == 3) //user loses
{
if (userNum == 1) //user lost choosing rock
{
System.out.println("paper beats rock");
System.out.println("you lose");
}
else if (userNum == 2) //user lost choosing paper
{
System.out.println("scissors beats paper");
System.out.println("you lose");
}
else if (userNum == 3) //user lost choosing scissors
{
System.out.println("rock beats scissors");
System.out.println("you lose");
}
else if (result == 2) //draw
System.out.println("draw");
}
System.out.println("would you like to play again?");
System.out.println("1 = yes");
System.out.println("2 = no");
input = keyboard.nextInt();
keepPlaying = play(input);
} while (keepPlaying == true);
}
// method 1 (menu)
public static void menu()
{
System.out.println("Enter your choice of rock, paper, or scissors\n" + "1 = rock\n" + "2 = paper\n" + "3 = scissors");
}
// method 2 (result check)
public static int resultCheck(int userNum, int compNum)
{
if (userNum == 2 && compNum == 1)
return 1;
else if (userNum == 1 && compNum == 3)
return 1;
else if (userNum == 3 && compNum == 2)
return 1;
else if (userNum == compNum)
return 2;
else
return 3;
}
// method 3 (converting number choice to rock/paper/scissors
public static String numToChoice(int num)
{
if (num == 1)
return "rock";
else if (num == 2)
return "paper";
else if (num == 3)
return "scissors";
}
//method 4 (play again)
public static boolean play(int input)
{
if (input == 1)
return true;
else if (input == 2)
return false;
}
}
numToChoice(4)? Andplay(7)? What are those methods supposed to return then?else return "invalid";behind that or something like that.