我正在编写一个应该模拟掷骰子的程序,据我所知,之后没有做出重大更改,程序似乎已停止使用我提供的用户输入。知道为什么吗?
更新:错误仅在骰子滚动不等于7或11时发生。
import java.io.*;
import java.util.*;
public class Craps {
public static void main(String[] args) {
int bet=0;
int availableMoney=1000;
Scanner userInput=new Scanner(System.in);
int diceValue=0;
int die1=0;
int die2=0;
int numberTryingFor=0;
Random diceNumber=new Random();
boolean isItTheBeginningOfTheRound=true;
System.out.println("Welcome to craps!");
System.out.println("You have "+availableMoney+" dollars available.");
while(availableMoney>0){
//sets up round
System.out.println("Please input a bet.");
bet=userInput.nextInt();
die1=1+diceNumber.nextInt(6);
die2=1+diceNumber.nextInt(6);
diceValue=die1+die2;
/*System.out.println(die1);
System.out.println(die2);
System.out.println(diceValue);
System.out.println(availableMoney);*/
if(diceValue==7||diceValue==11&&isItTheBeginningOfTheRound){
availableMoney=availableMoney+bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You win! You now have "+availableMoney+" dollars available.");
}else if(diceValue==2||diceValue==3||diceValue==12&&isItTheBeginningOfTheRound){
availableMoney=availableMoney-bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You lost. You now have "+availableMoney+" dollars available.");
}else{
diceValue = tryForPoint(diceValue, numberTryingFor, diceNumber);
if(diceValue==7&&!isItTheBeginningOfTheRound){
availableMoney=availableMoney-bet;
System.out.println("Your roll is "+diceValue);
System.out.println("Uh Oh! You lost. You now have "+availableMoney+"left.");
}else{
availableMoney=availableMoney+bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You won!You now have "+availableMoney);
}
}
}
System.out.println("You are out of money. You lose.");
}
public static int tryForPoint(int diceValue, int numberTryingFor,
Random diceNumber) {
int die1;
int die2;
while(diceValue!=7||numberTryingFor!=diceValue){
die1=1+diceNumber.nextInt(6);
die2=1+diceNumber.nextInt(6);
diceValue=die1+die2;}
return diceValue;
}
}
答案 0 :(得分:2)
你永远不会改变isItTheBeginningOfTheRound的值,所以辅助块永远不会执行,因为
! isItTheBeginningOfTheRound总是假的。