简体   繁体   English

while循环内的if / else语句

[英]if/else statement inside of a while loop

I've written a code for a game that simulates the user and the computer rolling a die and the winner receives 1$ from the loser, with each starting with 2$. 我已经为游戏编写了一个代码,该游戏模拟了用户和计算机掷骰子,获胜者从失败者那里获得1美元,每个都以2美元开始。 The code runs fine, but it doesn't end when either the user or computer reaches 0$ like i had anticipated. 该代码运行良好,但是当用户或计算机达到0 $时,它并没有结束,正如我所预期的那样。 Any suggestions? 有什么建议么?

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);
    }}

您的while语句正在测试<= 0,但最初两个变量的值都>0。while循环永远不会触发。

First off you have a problem with your while condition money values = 2 for player and comp, so while will never fire..fix that and you could use a do while 首先,您的条件条件是玩家和comp的筹码值等于2,所以while永远不会触发..fix问题,您可以使用do

do{
    statement(s) //block of statements
}while (Boolean expression);

So inside your do{} you could have your statements and conditions..so it will do whatever is inside those braces until the condition inside the while() is met 因此,在do{}内部,您可以拥有语句和条件..以便它将执行括号内的所有操作,直到满足while()内部的条件为止

for example 例如

class DoWhileLoopExample {
    public static void main(String args[]){
         int i=10;
         do{
              System.out.println(i);
              i--;
         }while(i>1);
    }
}

您希望在小于或等于但大于或等于时停止。

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

Cause of problem: 问题原因:

Your while loop does not run at all, because the condition inside is initially evaluated as false. 您的while循环根本不会运行,因为内部条件最初被评估为false。

  while (userMoney <= 0 || compMoney <=0)

This reads: whilst the user has a money less than or equal to 0 OR whilst the computer has money less than or equal to 0 then run the while loop, this will never initially be true as they both the computer and user both start with $2, hence userMoney == 2 and compMoney == 2. 内容如下:尽管用户的钱少于或等于0,或者计算机的钱少于或等于0,然后运行while循环,但这最初不会是正确的,因为他们的计算机和用户都以$ 2开头,因此userMoney == 2和compMoney == 2。


Solution: 解:

Change the condition of the while loop to the following: 将while循环的条件更改为以下内容:

 while(userMoney>0 || compMoney>0)

then add an if statement which says if the compMoney == 0 or if the userMoney == 0, then break out of the while loop. 然后添加一条if语句,说明compMoney == 0或userMoney == 0,然后退出while循环。

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

You are using the incorrect condition in the while statement.You are testing if any player has >=0 this will always test true and cause an infinite loop. 您在while语句中使用的条件不正确。您正在测试是否有任何玩家的游戏>> = 0,这将始终测试为真并导致无限循环。 Instead test if BOTH players have >0 and end the game if not. 而是测试两个玩家的筹码是否都大于0,如果没有,则结束游戏。

Also you have a ';' 你也有一个“;” after you if statement. 在您之后if语句。 That will cause the code after it to execute all the time. 这将导致代码始终执行。

Here is complete working code: 这是完整的工作代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM