Source 1978 History I package project3; 1 w NP 2 3 4 @author 5 7 import java.util.Scanner; 60000 8 9 10 11 모 12 13 publi

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Source 1978 History I package project3; 1 w NP 2 3 4 @author 5 7 import java.util.Scanner; 60000 8 9 10 11 모 12 13 publi

Post by answerhappygod »

 1
1 (54.99 KiB) Viewed 30 times
How do I exit the while loop so that it outputs "scissors (0),
rock (1), paper (2), exit (-1) : " again and accepts an input
instead of ending the program. The program should repeatedly
ask the user to input either "scissors (0), rock (1), paper (2),
exit (-1) : " until the user enters -1 and thats when it should
display the "program results" under the previous outputs.
Right now the code only asks the user to input it once, and then
stops.
Source 1978 History I package project3; 1 w NP 2 3 4 @author 5 7 import java.util.Scanner; 60000 8 9 10 11 모 12 13 public class project3v2 { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("scissors (0), rock (1), paper (2), exit (-1) int usersChoice = input.nextInt(); : "); 14 15 16 17 18 19 int totalGames = 0; int playerWins = 0; int computerWins = 0; int draws = 0; 20 21 22 23 24 int computersChoice = (int) (Math.random() * 3); while (usersChoice >= -1){ N N N N N N 25 26 27 28 29 30 31 cou No 1 co o en NooCo if(usersChoice >= 2) System.out.print("You are paper."); else if (usersChoice >= 1 && usersChoice < 2) System.out.print("You are rock."); else if (usersChoice <1 && usersChoice >= 0) System.out.print("You are scissors."); 32 3 33 34 35 لا لا لا لا لا لا if (computersChoice >= 2) System.out.print(" The computer is paper."); else if (computersChoice >= 1 && computersChoice < 2) System.out.print(" The computer is rock."); else if (computersChoice <i && computersChoice >= 0) System.out.print(" The computer is scissors."); 36 37 38 39 40 41 42 k 43 44 45 if (computersChoice == 0 && usersChoice == 1) { System.out.println(" You won."); playerWins++; totalGames++; break; } else if (computersChoice == 1 && usersChoice == 0) { System.out.println(" You lose."); computerWins++; totalGames++; break; 46 47 48 000 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 } else if (computersChoice == 2 && usersChoice == 1) { System.out.println(" You lose."); computerWins++; totalGames++; break; } else if (computersChoice == 2 && usersChoice == 0) { System.out.println(" You won."); playerWins++; totalGames++; break; } else if (computersChoice == 0 && usersChoice == 1) { System.out.println(" You won."); playerWins++; totalGames++; break; } else if (computersChoice == 0 && usersChoice == 2) { System.out.println(" You lose."); computerWins++; totalGames++; break; } else { System.out.print(" It is a draw."); draws++; totalGames++; break; } 65 66 67 68 69 70 71 72 73 74 75 76 77 78 } 79 80 81 82 0001 if (usersChoice <= -1){ System.out.println("\nProgram Results"); System.out.println("- "); System.out.println("Rounds played: " + totalGames); System.out.println("Player wins: " + playerWins); System.out.println("Computer wins: + computerWins); System.out.println("Draws: " + draws); } TE 83 84 85 86 87 un lo } } 88 89 90
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply