我为游戏编写了一个模拟用户和计算机滚动游戏的代码,获胜者从失败者那里获得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);
}}
答案 0 :(得分:1)
你的while语句正在测试&lt; = 0,但最初两个变量都是&gt; 0. while循环永远不会触发。
答案 1 :(得分:1)
首先你有一个问题,你的条件金钱值= 2为球员和comp,所以虽然永远不会解雇..修复那你可以使用一个做什么
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);
}
}
答案 2 :(得分:0)
您希望在小于或等于但是大于或等于时停止。
while (userMoney >= 0 || compMoney >=0) {
答案 3 :(得分:0)
您的while循环根本不运行,因为内部条件最初被评估为false。
while (userMoney <= 0 || compMoney <=0)
这是:当用户有小于或等于0的钱时,或者当计算机有小于或等于0的钱时,然后运行while循环,这将永远不会是真的,因为他们都是计算机并且用户都以$ 2开头,因此userMoney == 2和compMoney == 2.
将while循环的条件更改为以下内容:
while(userMoney>0 || compMoney>0)
然后添加一个if语句,说明compMoney == 0或者userMoney == 0,然后跳出while循环。
if(userMoney == 0 || compMoney == 0){
break;
}
答案 4 :(得分:0)
您在while语句中使用的条件不正确。您正在测试是否有任何播放器具有&gt; = 0这将始终测试为true并导致无限循环。而是测试 BOTH 玩家是否> 0并且如果没有则结束游戏。
你也有一个&#39 ;;&#39;在你之后如果声明。这将导致其后的代码一直执行。
这是完整的工作代码:
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