Hi, I am writting a java rock paper scisssors code, and the code that i have so
ID: 3543287 • Letter: H
Question
Hi, I am writting a java rock paper scisssors code, and the code that i have so far works, i just need a while statement that states once computer or player wins twice, they win. and i also i need a statement that says who won, and displays the 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
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.");
System.out.print(" Player1,please choose rock, paper, or scissors ");
while(compcount<2&&player1count<2){
computerchoice = (int) (Math.random() * 3);
if (computerchoice == 0)
compchoice = "Rock";
else if (computerchoice == 1)
compchoice = "Paper";
else
compchoice = "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++;
}
System.out.print(" No one won game ..continue playing .... Player1,please choose rock, paper, or scissors ");
}
if(compcount==2) System.out.print(" System won ");
else System.out.print(" you won");
System.out.print(" computer won "+compcount+" number of times " + " usser won "+player1count+" number of times ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.