Java while循环中的if/else语句

Java while循环中的if/else语句,java,if-statement,while-loop,Java,If Statement,While Loop,我为一个游戏编写了一个代码,模拟用户和计算机掷骰子,胜利者从失败者那里得到1美元,每个从2美元开始。代码运行良好,但当用户或计算机达到我预期的0$时,代码并没有结束。有什么建议吗 import java.util.Scanner; public class ALE_04_RollDice { public static void main (String[] args) { Scanner input = new Scanner(System.in); int userMon

我为一个游戏编写了一个代码,模拟用户和计算机掷骰子,胜利者从失败者那里得到1美元,每个从2美元开始。代码运行良好,但当用户或计算机达到我预期的0$时,代码并没有结束。有什么建议吗

import java.util.Scanner;

public class ALE_04_RollDice {
public static void main (String[] args) {

    Scanner input = new Scanner(System.in);
    int userMoney = 2;
    int compMoney = 2;
    int userRoll = (int) (1 + Math.random() * 6);
    int compRoll = (int) (1 + Math.random() * 6);

    System.out.print("Press 'r' if you would like to roll ");
    do {String roll = input.nextLine();
    if (roll.equals("r")); {
        if (userRoll > compRoll){
            userMoney++;
            compMoney--;
     System.out.print("The computer rolled " + compRoll + " and you rolled "       + userRoll + ". you won."
        + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
            }

            else if (userRoll < compRoll) {
                compMoney++;
                userMoney--;
                System.out.print("The computer rolled " + compRoll + " and you rolled " + userRoll + 
                        ". you lost" + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
            }

            else {System.out.print("The computer rolled " + compRoll + "and you rolled " + userRoll + 
                    ". it's a tie" + "\n" + "You have $" + userMoney + " & the computer has $" + compMoney);}


            }
            }while (userMoney >= 0 || compMoney >=0);
    }}
import java.util.Scanner;
公共级ALE_04_RollDice{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int userMoney=2;
国际货币基金组织=2;
int userRoll=(int)(1+Math.random()*6);
int compRoll=(int)(1+Math.random()*6);
System.out.print(“如果要滚动,请按“r”);
do{String roll=input.nextLine();
如果(滚动等于(“r”){
如果(用户滚动>压缩){
用户货币++;
钱——;
System.out.print(“计算机滚动”+compRoll+“而你滚动”+userRoll+”。你赢了。”
+“\n”+“您有$”+userMoney+“&计算机有$”+compMoney);
}
否则如果(用户滚动=0 | | compMoney>=0);
}}

当小于或等于但有大于或等于时,您希望停止

    while (userMoney >= 0 || compMoney >=0) {

您的while语句正在测试0。while循环永远不会触发。

首先,玩家和玩家的while条件money value=2存在问题,因此while将永远不会触发。。解决了这个问题,您可以使用do-while

do{
    statement(s) //block of statements
}while (Boolean expression);
因此,在
do{}
中,您可以有自己的语句和条件。因此,它将执行这些大括号中的任何操作,直到满足
while()
中的条件

比如说

class DoWhileLoopExample {
    public static void main(String args[]){
         int i=10;
         do{
              System.out.println(i);
              i--;
         }while(i>1);
    }
}
问题原因: while循环根本不运行,因为其中的条件最初被评估为false。

  while (userMoney <= 0 || compMoney <=0)
然后添加一个if语句,说明compMoney==0或userMoney==0,然后中断while循环

 if(userMoney == 0 || compMoney == 0){
     break;
}

您在while语句中使用了不正确的条件。您正在测试是否有任何播放机大于等于0,这将始终测试true并导致无限循环。相反,测试两个玩家是否都有>0,如果没有,则结束游戏

你还有一个“;”在你发表声明之后。这将导致后面的代码一直执行

以下是完整的工作代码:

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int userMoney = 2;
int compMoney = 2;
int userRoll = (int) (1 + Math.random() * 6);
int compRoll = (int) (1 + Math.random() * 6);

System.out.print("Press 'r' if you would like to roll ");
do {String roll = input.nextLine();
if (roll.equals("r")) {
    if (userRoll > compRoll){
        userMoney++;
        compMoney--;
 System.out.print("The computer rolled " + compRoll + " and you rolled "       + userRoll + ". you won."
    + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else if (userRoll < compRoll) {
            compMoney++;
            userMoney--;
            System.out.print("The computer rolled " + compRoll + " and you rolled " + userRoll + 
                    ". you lost" + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else {System.out.print("The computer rolled " + compRoll + "and you rolled " + userRoll + 
                ". it's a tie" + "\n" + "You have $" + userMoney + " & the computer has $" + compMoney);}


        }
         //prompt user to type 'r' to roll again
        System.out.println("\n\nPress 'r' if you would like to roll ");
        }while (userMoney > 0 && compMoney >0);

        System.out.println("\n\nGAME OVER!!!");
    }
   }
//end main
publicstaticvoidmain(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int userMoney=2;
国际货币基金组织=2;
int userRoll=(int)(1+Math.random()*6);
int compRoll=(int)(1+Math.random()*6);
System.out.print(“如果要滚动,请按“r”);
do{String roll=input.nextLine();
如果(滚动等于(“r”)){
如果(用户滚动>压缩){
用户货币++;
钱——;
System.out.print(“计算机滚动”+compRoll+“而你滚动”+userRoll+”。你赢了。”
+“\n”+“您有$”+userMoney+“&计算机有$”+compMoney);
}
否则如果(用户滚动0&&compMoney>0);
System.out.println(“\n\n名称超过!!!”;
}
}
//端干管

如果这解决了您的问题,您应该接受答案
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int userMoney = 2;
int compMoney = 2;
int userRoll = (int) (1 + Math.random() * 6);
int compRoll = (int) (1 + Math.random() * 6);

System.out.print("Press 'r' if you would like to roll ");
do {String roll = input.nextLine();
if (roll.equals("r")) {
    if (userRoll > compRoll){
        userMoney++;
        compMoney--;
 System.out.print("The computer rolled " + compRoll + " and you rolled "       + userRoll + ". you won."
    + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else if (userRoll < compRoll) {
            compMoney++;
            userMoney--;
            System.out.print("The computer rolled " + compRoll + " and you rolled " + userRoll + 
                    ". you lost" + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else {System.out.print("The computer rolled " + compRoll + "and you rolled " + userRoll + 
                ". it's a tie" + "\n" + "You have $" + userMoney + " & the computer has $" + compMoney);}


        }
         //prompt user to type 'r' to roll again
        System.out.println("\n\nPress 'r' if you would like to roll ");
        }while (userMoney > 0 && compMoney >0);

        System.out.println("\n\nGAME OVER!!!");
    }
   }
//end main