我应该在java中制作胡扯游戏,但是我有一个问题。你看,技术上这场比赛已经结束了,但是即使你这样做,也不会说你是否放弃了比赛。它只会继续滚动直到你赢。我尝试了一些解决方法,但它似乎只是将其陷入无限循环。游戏规则: 掷两个骰子。每个管芯具有分别代表值1,2,...和6的六个面。 检查两个骰子的总和。如果总数是2,3或12(称为掷骰子),那么你输了;如果总数是7或11(称为自然),你就赢了;如果总和是另一个值 (即,4,5,6,8,9或10),则建立一个点。继续滚动骰子,直到 a 7或相同的积分值滚动。如果7滚动,你就输了。否则,你赢了。胡扯游戏Java
Example run:
You rolled 4 + 4 = 8
point is 8
You rolled 6 + 2 = 8
You win
这里是我的代码如下:
import java.util.*;
public class CrapsGame
{
public static void main (String[]args)
{
String restart = "y";
Scanner scan = new Scanner(System.in);
int sum=rollDice();
int points = points(sum);
boolean youWin=youWin(sum, points);
while(restart.equals("y")){
youWin=false;
while(youWin==false){
rollDice();
sum=rollDice();
points=points(sum);
youWin=youWin(sum, points);
}
System.out.print("\nWould you like to play again? y or n: ");
restart = scan.next();
}
System.out.print("The program has ended!");
}
public static int rollDice()
{
int num1= (int)(6.0*Math.random() + 1.0); //first die
int num2= (int)(6.0*Math.random() + 1.0); //second die
int sum= num1 + num2; //sum of roll
System.out.printf("\nYou have rolled %d + %d = %d\n", num1, num2, sum); //Prints the sum
return sum;
}
public static int points(int sum)
{
int points=0;
if (sum>=4 && sum<=6) { //Counts points based on your rolls
points = points + 1;
System.out.print("Your points are: " + points);
}
else if (sum>=8 && sum<=10){
points = points + 1;
System.out.print("Your points are: " + points);
}
return points;
}
public static boolean youWin(int sum, int points)
{
boolean youWin=false;
if (sum==2 || sum==3 || sum == 12) {
youWin=false;
System.out.print("You lost with a " + sum); //Determines if you win or loose based on the sum and points and returns the youWin boolean
}
else if (sum==7 || sum==11) {
youWin=true;
System.out.print("You won with a " + sum);
}
else if (points==7){
youWin=true;
}
return youWin;
}
}
1.删除代码的所有噪音。删除所有那些与你的问题无关的无意义评论和代码。 2.你应该清楚你的行为如何偏离预期的行为。 3.学会使用调试器并追踪它。通常这种问题很容易在调试器的帮助下发现 –
我会这样做的,我必须在那里有所有的废话,因为这是我的教授希望我拥有的东西。我知道这没有帮助。 – snipem1438
你会在你的家庭作业中需要这些,但不是在这里的问题。 –