我试图在计算机或玩家赢得5场比赛之后终止循环。我尝试用for循环来做到这一点,但它根本没有停止。我试图调试并搞清楚但我迷路了。 能帮我弄清楚哪里出错了吗?任何使代码更高效的建议也将非常受欢迎。
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissor {
public static void main (String [] args){
System.out.println("Lets play Rock, Paper, Scissors!");
Scanner scanner = new Scanner(System.in);
int computerwins=0;
int playerwins=0;
Random random = new Random();
for (int totalGame = 0; playerwins<=5 || computerwins<=5; ++totalGame){
int gamenumber = totalGame + 1;
System.out.print("Game " + gamenumber + ". " + "Pick one! 1 = Rock, 2 = Paper, 3 = Scissor : ");
int input = scanner.nextInt();
Random rand = new Random();
int computerResponse = rand.nextInt(3) + 1;
String compResponse = "something";
switch (computerResponse) {
case 1: compResponse = "Rock";
break;
case 2: compResponse = "Paper";
break;
case 3: compResponse = "Scissor";
break; }
String you = "something";
switch (input) {
case 1: you = "Rock";
break;
case 2: you = "Paper";
break;
case 3: you = "Scissor";
break;
}
System.out.println("You: " + you + ", Computer: " + compResponse);
if(input == computerResponse) {
System.out.println("It's a tie!"); }
else if (input == 1) {
if (computerResponse == 2) {
System.out.println("Paper eats rock. You lose!");
++computerwins; }
else if (computerResponse == 3) {
System.out.println("Rock crushes scissors. You win!");
++playerwins; } }
else if (input == 2) {
if (computerResponse == 1) {
System.out.println("Paper eats rock. You win!");
++playerwins; }
else if (computerResponse == 3) {
System.out.println("Scissor cuts paper. You lose!");
++computerwins; } }
else if (input ==3) {
if (computerResponse == 1) {
System.out.println("Rock crushes scissors. You lose!");
++computerwins; }
else if (computerResponse == 2) {
System.out.println("Scissor cuts paper. You win!");
++playerwins; } }
System.out.println("Score Now: You = " + playerwins + ", Computer = " + computerwins );
System.out.println(" "); }
if (playerwins == computerwins) {
System.out.println("Aww! It's a tie."); }
if (playerwins < computerwins) {
System.out.println("Aww! You lost!"); }
if (playerwins > computerwins) {
System.out.println("Yay! You won!");}
} }
谢谢!
答案 0 :(得分:0)
在你的for循环终止条件
playerwins<=5 || computerwins<=5
只要EITHER条件为真,这就会继续。您希望将其更改为&&
以继续,直到一个条件变为false。