how come this program only loops twice? wheres my mistake?
hey guys so i been asked to write this rock paper scissors program. and it should loop until whenever either player or computer reaches 3 wins. however, it only loops twice for some reason here's my code and thanks in advance.
Code Java:
import java.util.Scanner;
import javax.swing.JOptionPane;
class RockPaperScissors
{
public static void main(String args[])
{
int compWin = 0;
int userWin = 0;
Scanner input = new Scanner(System.in);
JOptionPane.showMessageDialog(null,
"ROCK - PAPER - SCISSORS" + " \n Whoever wins more than two times is the winner!");
do
{
int totalRound = 1;
String s = JOptionPane.showInputDialog(null,
"Computer's Move: X" + "\n Your Move: ",
"Scissor-Rock-Paper",
JOptionPane.QUESTION_MESSAGE);
int player = Integer.parseInt(s);
if ( player < 3)
{
int computer = (int)(Math.random() * 3);
if (computer == player)
{
JOptionPane.showMessageDialog(null, "it's a tie make your move again");
}
else if ( computer == 0 && player == 2 )
{
JOptionPane.showMessageDialog(null, "scissor0 cuts paper2 computer wins");
compWin++;
}
else if ( computer == 0 && player == 1)
{
JOptionPane.showMessageDialog(null, " rock1 beats scissor0 player wins");
userWin++;
}
else if ( computer == 1 && player == 0)
{
JOptionPane.showMessageDialog(null, " rock1 beats scissor0 computer wins");
compWin++;
}
else if ( computer == 1 && player == 2)
{
JOptionPane.showMessageDialog(null, " paper2 beats rock1 player wins");
userWin++;
}
else if ( computer == 2 && player == 0)
{
JOptionPane.showMessageDialog(null, " scissor0 cuts paper2 player wins");
userWin++;
}
else if ( computer == 2 && player == 1)
{
JOptionPane.showMessageDialog(null, "paper2 beats rock1 computer wins");
compWin++;
}
totalRound++;
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! No such choice. Please enter (0) for Scissor, (1) for Rock, and (2) for paper");
}
JOptionPane.showInputDialog(null,
"Computer's Move: X" + "\n Your Move: ",
"Scissor-Rock-Paper",
JOptionPane.QUESTION_MESSAGE);
}while (compWin > 2 || userWin > 2);
}
}
Re: how come this program only loops twice? wheres my mistake?
Quote:
Originally Posted by
Rainy
hey guys so i been asked to write this rock paper scissors program. and it should loop until whenever either player or computer reaches 3 wins. however, it only loops twice for some reason here's my code and thanks in advance.
do
{...
}
while (compWin > 2 || userWin > 2);
}
your loop doesn't loop even one times you think that it loops twice because you used "do while" and you used two Input JOptionpanes.
Code java:
while(compWin > 2 || userWin > 2)
means loop until compWin or userWin bigger than 2
use while(compWin<3 && userWin<3)
this will loop until both compWin and userWin<3
Re: how come this program only loops twice? wheres my mistake?
Quote:
Originally Posted by
serdar
your loop doesn't loop even one times you think that it loops twice because you used "do while" and you used two Input JOptionpanes.
Code java:
while(compWin > 2 || userWin > 2)
means loop until compWin or userWin bigger than 2
use while(compWin<3 && userWin<3)
this will loop until both compWin and userWin<3
OMG thank you so much!!! i feel so dumb right now, shouldn't make this kind of mistakes haha. thanks again!