我创造了3种不同的方法。我假设第一个使用不会返回和输出,但有可能所有这些都不是。我不明白为什么。这是我知道的大量代码,但我想添加整个内容,因为我不确定是什么导致这种情况发生。当我运行程序时,它不会在屏幕上打印任何内容,也不会停止运行。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int currentPool = 100;
while (currentPool > 0);{
int bet = getBet(in, currentPool);
if (bet == 0){
System.out.println("You have " + currentPool + " dollars left.");
System.out.println("goodbye!");
currentPool = 0;
}
else{
char highLow = getHighLow(in);
int roll = getRoll();
int winnings = determineWinnings(highLow, bet, roll);
currentPool = currentPool + winnings;
}
}
}
private static int getBet(Scanner inScanner, int currentPool) {
System.out.println("You have " + currentPool + " dollars.");
System.out.println("Enter an amount to bet (0 to quit): ");
int bet = inScanner.nextInt();
while (bet > currentPool || bet < 0){
System.out.println("Your bet MUST be between 0 and " + currentPool + " dollars.");
System.out.println("You have " + currentPool + " dollars.");
System.out.println("Enter an amount to bet (0 to quit): ");
bet = inScanner.nextInt();
}
return bet;
}
private static char getHighLow(Scanner inScanner) {
System.out.println("High, Low, or Sevens (H/L/S): ");
String highLowSevens = inScanner.nextLine();
highLowSevens = highLowSevens.toUpperCase();
while (!highLowSevens.equals("H") && !highLowSevens.equals("L") && !highLowSevens.equals("S")){
System.out.println("That is not one of the options!");
System.out.println("High, Low, or Sevens (H/L/S): ");
highLowSevens = inScanner.nextLine();
}
char hLS = highLowSevens.charAt(0);
return hLS;
}
private static int getRoll() {
int roll = (int)(6 * Math.random() + 1);
return roll;
}
private static int determineWinnings(char highLow, int bet, int roll) {
if (((highLow == 'H') && (roll > 7)) || ((highLow == 'L') && (roll < 7))){
return bet;
}
else{
if (((highLow == 'S') && (roll == 7))){
return (bet*4);
}
else{
return (bet*(-1));
}
}
}
答案 0 :(得分:3)
更改
while (currentPool > 0);
要
while (currentPool > 0)
在你的主要方法中。