Java循环错误-需要重新审视
我正在为Java类编写一个简单的craps模拟器,由于某些原因,它在正确运行时遇到了问题。它应该用“点”来记录损失,用“点”来记录胜利,但出于某种原因,这些值每次都趋向于1或0。第一轮的输赢似乎起了作用。想知道有没有一个有着新眼睛的人能知道我把事情搞砸了。谢谢大家!Java循环错误-需要重新审视,java,loops,flow,Java,Loops,Flow,我正在为Java类编写一个简单的craps模拟器,由于某些原因,它在正确运行时遇到了问题。它应该用“点”来记录损失,用“点”来记录胜利,但出于某种原因,这些值每次都趋向于1或0。第一轮的输赢似乎起了作用。想知道有没有一个有着新眼睛的人能知道我把事情搞砸了。谢谢大家! import java.util.Scanner; import java.util.Random; class CrapsSimulator { public static void main(String[] args)
import java.util.Scanner;
import java.util.Random;
class CrapsSimulator {
public static void main(String[] args) {
// Set up values we will use
int lossfirstroll = 0;
int winfirstroll = 0;
int losswithpoint = 0;
int winwithpoint = 0;
boolean gameover = false;
int point = 0;
// Loop through a craps game 100 times
for (int i = 0; i < 100; i++) {
// First roll -- random number within 2-12
Random rand = new Random();
int random = rand.nextInt(11) + 2;
// Win on first roll
if (random == 7 || random == 11) {
winfirstroll++;
gameover = true;
} // Loss on first roll
else if (random == 2 || random == 3 || random == 12) {
lossfirstroll++;
gameover = true;
} else // Player has "point"
{
point = random;
}
// Check to make sure the game hasn't ended already
while (gameover == false) {
// Reroll the dice
random = rand.nextInt(11) + 2;
// Check to see if player has won
if (random == point) {
winwithpoint++;
gameover = true;
}
// Or if the player has lost
if (random == 7) {
losswithpoint++;
gameover = true;
}
// Otherwise, keep playing
gameover = false;
}
}
// Output the final statistics
System.out.println("Final Statistics\n");
System.out.println("Games played: 100\n");
System.out.println("Wins on first roll: " + winfirstroll + "\n");
System.out.println("Losses on first roll: " + lossfirstroll + "\n");
System.out.println("Wins with point: " + winwithpoint + "\n");
System.out.println("Losses with point: " + losswithpoint + "\n");
}
}
import java.util.Scanner;
导入java.util.Random;
类仿真器{
公共静态void main(字符串[]args){
//设置我们将使用的值
int lossfirwalk=0;
int winfirstroll=0;
int losswithpoint=0;
int winwithpoint=0;
布尔gameover=false;
int点=0;
//在掷骰子游戏中循环100次
对于(int i=0;i<100;i++){
//第一卷——2-12范围内的随机数
Random rand=新的Random();
int random=rand.nextInt(11)+2;
//第一轮获胜
如果(随机==7 | |随机==11){
winfirstroll++;
gameover=true;
}//第一卷损失
else if(random==2 | | random==3 | | random==12){
lossfirwalk++;
gameover=true;
}否则//玩家有“点”
{
点=随机;
}
//检查以确保游戏尚未结束
while(gameover==false){
//重新掷骰子
random=rand.nextInt(11)+2;
//检查玩家是否赢了
如果(随机==点){
winwithpoint++;
gameover=true;
}
//或者如果玩家输了
如果(随机==7){
losswithpoint++;
gameover=true;
}
//否则,继续玩
gameover=false;
}
}
//输出最终统计数据
System.out.println(“最终统计\n”);
System.out.println(“玩的游戏:100\n”);
System.out.println(“第一卷时获胜:+winfirstroll+”\n);
System.out.println(“第一卷损失:+lossfirtwark+”\n”);
System.out.println(“以点数取胜:+winwithpoint+”\n);
System.out.println(“带点的损失:+losswithpoint+”\n”);
}
}
通过调试器运行它,或者喷洒System.out.println
查看逻辑失败的地方。这是家庭作业吗 通过调试器运行它,或者喷洒System.out.println
查看逻辑失败的地方。这是家庭作业吗 您的问题是gameover
标志。您总是在内部循环结束时再次将其设置为false
,这将使其永远运行。您的问题是gameover
标志。您总是在内部循环结束时再次将其设置为false
,这将使其永远运行。我会将new Random()
移到for循环之外,以防这会影响随机数的熵。@darvids0n这是一个很好的建议,但没有帮助。不过我很感激。我会把new Random()
移到for循环之外,以防万一这会影响随机数的熵。@darvids0n这是一个很好的建议,但没有帮助。尽管如此,我还是很感激。OP说他正在为Java类编写程序,
,这一点应该很清楚。我还想说,使用调试器应该是第一件事。在“point=random”行中添加一个断点应该可以让一些事情变得清楚。是的。这是家庭作业。但这又有什么区别呢?我写了所有的资料。它只是在某个地方遇到了麻烦,我想请你们“重新审视一下”,也许能看到我错过的错误。OP说他正在为Java类编写程序,
,这一点应该很清楚。我还想说,使用调试器应该是第一件事。在“point=random”行中添加一个断点应该可以让一些事情变得清楚。是的。这是家庭作业。但这又有什么区别呢?我写了所有的资料。它只是在某个地方遇到了麻烦,我向你们寻求“新鲜的眼睛”,也许能看到我错过的错误。对不起。这是我调试时的一个打字错误。不过眼睛不错:)我需要在for循环开始时将gameover设置为false。谢谢。事实上,在编码时,您可能希望在for循环结束时将其设置为false,而不是在while循环结束时。那时候就行了。但是,在每次迭代开始时指定它会更干净。对不起。这是我调试时的一个打字错误。不过眼睛不错:)我需要在for循环开始时将gameover设置为false。谢谢。事实上,在编码时,您可能希望在for循环结束时将其设置为false,而不是在while循环结束时。那时候就行了。但是,在每次迭代开始时指定它会更干净。