methods - trouble getting results from rock paper scissors code (java) -
so when run code, keeps saying tie because think there problem in 1 of methods.
here whole code:
string userchoice = ""; string computerchoice = ""; system.out.println("welcome rock, paper, scissors."); system.out.println("the rules of game rock breaks scissors, " + "scissors cut paper, , paper covers rock. in game, " + "the user play against computer."); system.out.println("the legend game is: r = rock, p = paper," + " , s = scissors."); generateuserchoice(); generatecomputerchoice(); determiningoutcome(userchoice, computerchoice); repeatgame(userchoice, computerchoice); } public static string generatecomputerchoice() { string computerchoice = ""; int computerintchoice; random generator = new random(); computerintchoice = generator.nextint(3) + 1; if (computerintchoice == 1) { computerchoice = "r"; } else if (computerintchoice == 2) { computerchoice = "p"; } else if (computerintchoice == 3) { computerchoice = "s"; } system.out.println("the computer played " + computerchoice); return computerchoice; } public static string generateuserchoice() { string userchoice; scanner input = new scanner(system.in); system.out.println("please enter choice:"); userchoice = input.nextline(); userchoice = userchoice.touppercase(); return userchoice; } public static void determiningoutcome(string userchoice, string computerchoice) { if (userchoice.equals(computerchoice)) { system.out.println("it tie!"); } else if (userchoice.equalsignorecase("r") && computerchoice.equalsignorecase("s")) { system.out.println("rock beats scissors, win!!"); } else if (userchoice.equalsignorecase("p") && computerchoice.equalsignorecase("r")) { system.out.println("paper beats rock, win!!"); } else if (userchoice.equalsignorecase("s") && computerchoice.equalsignorecase("p")) { system.out.println("scissors beats paper, win!!"); } else if (computerchoice.equalsignorecase("r") && userchoice.equalsignorecase("s")) { system.out.println("rock beats scissors, lose."); } else if (computerchoice.equalsignorecase("p") && userchoice.equalsignorecase("r")) { system.out.println("paper beats rock, lose."); } else if (computerchoice.equalsignorecase("s") && userchoice.equalsignorecase("p")) { system.out.println("scissors beats paper, lose."); } else { system.out.println("sorry, invalid choice."); } } public static int repeatgame(string userchoice, string computerchoice) { int playagain; scanner input = new scanner(system.in); system.out.println("would play again? 1 = yes , 2 = no"); playagain = input.nextint(); if (playagain == 1) { system.out.println("would play again? 1 = yes , 2 = no"); generateuserchoice(); generatecomputerchoice(); determiningoutcome(userchoice, computerchoice); }else { system.out.println("thank playing!!"); } return playagain; } } however think problem in generatecomputerchoice part of code
thanks in advance
the problem userchoice , computerchoice been empty therefore tie.
you need assign userchoice , computerchoice return value of methods.
like this:
userchoice = generateuserchoice(); computerchoice = generatecomputerchoice();
Comments
Post a Comment