Hi, I am writting a java rock paper scisssors code, and the code this isthe code
ID: 3543323 • Letter: H
Question
Hi, I am writting a java rock paper scisssors code, and the code this isthe code i have so far works, i just need a while loop that will loop around until the computer or player wins twice, and will stop looping when who ever wins twice. also i need a statement that says who won, and displays the number of times they won. Can someone help me with this?
import java.util.Scanner;
public class Game {
public static void main(String[]args) {
int computerchoice = 0;
int compcount =0;
int player1count =0;
String compchoice;
String temp;
String player1;
int tiecount =0;
Scanner imput = new Scanner(System.in);
System.out.println("Welcome to the Rock-Paper-Scissors Game!");
System.out.println("The game is for two players who each choose one of rock, paper, or scissors.");
System.out.println("The winner is chosen by scissors cuts paper, rock breaks scissors, and paper wraps rock. ");
System.out.println("The program quits when one player has two wins.");
computerchoice = (int)(Math.random()*3);
if(computerchoice == 0)
compchoice = "Rock";
else if(computerchoice == 1)
compchoice = "Paper";
else
compchoice = "Scissors";
System.out.print(" Player1,please choose rock, paper, or scissors ");
player1 = imput.nextLine();
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("Paper covers rock, and you loose.");
compcount++;
}
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("scissors")){
player1count++;
System.out.print("rock, crushes scissors.");
}
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("rock")){
tiecount++;
System.out.print("Tie");
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("Scissors cuts paper, you win.");
player1count++;
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("rock")){
System.out.print(" Rock, crushes scissors, you lose");
compcount++;
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("scissors")){
System.out.print("tie.");
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("scissors")){
System.out.print("Scissors cuts paper, you lose.");
compcount++;
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("rock")){
System.out.print("paper covers rock, you win.");
player1count++;
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("tie");
tiecount++;
}
}
}
Explanation / Answer
package myPro;
import java.util.Scanner;
public class Game {
public static void main(String[]args) {
int computerchoice = 0;
int compcount =0;
int player1count =0;
String compchoice;
String temp;
String player1;
int tiecount =0;
Scanner imput = new Scanner(System.in);
System.out.println("Welcome to the Rock-Paper-Scissors Game!");
System.out.println("The game is for two players who each choose one of rock, paper, or scissors.");
System.out.println("The winner is chosen by scissors cuts paper, rock breaks scissors, and paper wraps rock. ");
while(true){
System.out.println("The program quits when one player has two wins.");
computerchoice = (int)(Math.random()*3);
if(computerchoice == 0)
compchoice = "Rock";
else if(computerchoice == 1)
compchoice = "Paper";
else
compchoice = "Scissors";
System.out.print(" Player1,please choose rock, paper, or scissors ");
player1 = imput.nextLine();
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("Paper covers rock, and you loose.");
compcount++;
}
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("scissors")){
player1count++;
System.out.print("rock, crushes scissors.");
}
if(player1.equalsIgnoreCase("rock")&& compchoice.equalsIgnoreCase("rock")){
tiecount++;
System.out.print("Tie");
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("Scissors cuts paper, you win.");
player1count++;
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("rock")){
System.out.print(" Rock, crushes scissors, you lose");
compcount++;
}
if(player1.equalsIgnoreCase("scissors")&& compchoice.equalsIgnoreCase("scissors")){
System.out.print("tie.");
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("scissors")){
System.out.print("Scissors cuts paper, you lose.");
compcount++;
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("rock")){
System.out.print("paper covers rock, you win.");
player1count++;
}
if(player1.equalsIgnoreCase("paper")&& compchoice.equalsIgnoreCase("paper")){
System.out.print("tie");
tiecount++;
}
if(player1count == 2){
System.out.print("Player Wins");
break;
}
if(compcount == 2){
System.out.print(" Computer Wins");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.