我做错了什么?掷骰子游戏 [英] What did I do wrong? Craps game

查看:102
本文介绍了我做错了什么?掷骰子游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来应该设计一个掷骰子的游戏,所以当你没有2,3,7,11或12时它让它工作,它会继续滚动,直到你达到你的观点或获得7. 7手段你失败了,如果你再次达到你的目标就赢了。但它一直在运行,说我滚了2.我做错了什么。非常困惑。我有什么在下面



我尝试过:



I am supposed to design a game of craps, so I had it working when you dont get a 2,3,7, 11, or 12, it will keep rolling until you reach your point or get a 7. 7 means you lose, if you hit your point again you win. But it keeps running and saying i rolled a 2. What did i do wrong. Very confused. what i have is below

What I have tried:

public class Program04 {
	

	public static void main(String[] args) {
		
		// C R A P S
		
		int roll1, roll2, firstTotal, sum;
		int point = 0;
		int newRoll1, newRoll2, newTotal;
		
		//Inputs
		System.out.println("Welcome to CRAPS! LETS PLAY!");
		
		//Compute and display
		roll1 = (int)(Math.random() * 6 + 1);
		roll2 = (int)(Math.random() * 6 + 1);
		firstTotal = roll1 + roll2;
		System.out.println("You rolled a " + roll1 + " and a " + roll2);
		System.out.println("You have a " + firstTotal);
		
		if (firstTotal == 7 || firstTotal == 11) {
			System.out.println("CONGRATS! YOU WON!");
		}
		else if (firstTotal == 2 || firstTotal == 3 || firstTotal == 12) {
			System.out.println("Too bad. You lost :(");
		}
		else {
			firstTotal = point;
			do {
			sum = rollDice();
			System.out.println("You rolled a " + sum + "!");
			
			} while (point != sum || point != 7);
			
			if (sum == 7) {
				System.out.println("You Lost!");
			}
			else if (sum == point) {
				System.out.println("You won! You got your sum to equal point!");
			}
				
		}

		
		
	}
	
	public static int rollDice() {
		int newVal1 = (int) Math.random() * 6 + 1;
		int newVal2 = (int) Math.random() * 6 + 1;
		int sum = newVal1 + newVal2;
		return sum;
		
	}

}

推荐答案

do {
	sum = rollDice();
	System.out.println("You rolled a " + sum + "!");

} while (point != sum || point != 7);



你没有离开这个循环,因为你从未在中存储任何值而不是初始值零。



建议:学习如何使用调试器。

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


You don't get out of this loop because you never store any value in point other than the initial zero.

Advice: learn how to use the debugger.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我做错了什么?掷骰子游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆