Reputation: 41
My counter is in a separate method from the main. I am trying to accumulate the number of wins, losses, and ties in multiple Rock-Paper-Scissors games. The counter only adds to the value once; as soon as I play the game again, the counter is reset and does not include results from the previous game...
do{
System.out.println("\n1=Rock\n2=Paper\n3=Scissors\n===========\nChoose:");
choice = Integer.parseInt(myInput.readLine());
determineOutcome();
System.out.println("\nYou have chosen " + ans);
System.out.println("The computer has chosen " + cans);
System.out.println("\nYOU'VE" + result);
System.out.println("\nWINS: " + win);
System.out.println("LOSSES: " + loss);
System.out.println("TIES: " + tie);
System.out.println("\nPress 1 to play again.");
loop = myInput.readLine();
}while("1".equals(loop));
} // end main method
public static void determineOutcome(){
// Randomize computer's choice (# between 1 and 3)
cchoice = (int)(Math.random()*3)+1;
// User's choice
switch ((int)choice)
{
case 1: ans = (" Rock.");
break;
case 2: ans = (" Paper.");
break;
case 3: ans = (" Scissors.");
break;
}
// Assign computer's number choice to a string
switch ((int)cchoice)
{
case 1: cans = (" Rock.");
break;
case 2: cans = (" Paper.");
break;
case 3: cans = (" Scissors.");
break;
}
win = 0;
loss = 0;
tie = 0;
if (choice == 1 && cchoice==3 || choice == 2 && cchoice == 1 || choice == 3 && cchoice == 2){
result = (" WON");
win++;
}
else if (choice == cchoice){
result = (" TIED");
tie++;
}
else if (choice == 1 && cchoice==2 || choice == 2 && cchoice == 3 || choice == 3 && cchoice == 1){
result = (" LOST");
loss++;
}
} // end determineOutcome(); method
Do I need to add a loop in the determineOutcome(); method? If so, where? I tried putting it around the entire thing and it did not work.
Upvotes: 0
Views: 998
Reputation: 6462
You do this every time you call the method:
win = 0;
loss = 0;
tie = 0;
I assume you have initialized the variables before calling the method, so dont set them to 0 every time you call the method. Remove this piece of code and it might just work :)
Upvotes: 1