我正在为我的编程课做作业,但是(再次)我的代码出了点问题,并且我花了一个半小时尝试调试它。该代码是一种赌博游戏,用户输入要下注的金额,然后选择高,低或七位数。高是指骰子掷骰数为8或更高,低是指骰子掷骰数为6或更低,而七是指骰子掷骰总数为7。如果骰子掷骰数为7,则下注乘以4并分配给用户。否则,当用户“输”时,他们将输掉所下注的金额。我似乎无法弄清楚到底是什么问题。
首先,奖金不正确。他们在控制台中显示为“您赢了128美元!”然后重复执行该代码时,即使用户迷失了本应扣除的金额,当前池也始终变为132。 此外,该程序会跳过defineWinnings方法中的“ if”语句-即使用户本应赢得,它也会默认设置为else。这是在20分钟内到期,我不知道该如何解决!衷心感谢您的帮助!
package example;
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
Scanner inScanner = new Scanner(System.in);
int currentPool = 100;
int bet = ' ';
char highLow = ' ';
int roll = ' ';
int winnings = ' ';
System.out.println("You have " + currentPool + " dollars.");
getBet(inScanner, currentPool);
getHighLow(inScanner);
determineWinnings(highLow, bet, roll);
currentPool = currentPool + winnings;
System.out.println("");
while (bet != 0)
{
System.out.println("You have " + currentPool + " dollars.");
getBet(inScanner, currentPool);
getHighLow(inScanner);
determineWinnings(highLow, bet, roll);
currentPool = currentPool + winnings;
System.out.println("");
}
}
private static int getBet(Scanner inScanner, int currentPool)
{
System.out.print("Enter an amount to bet (0 to quit): ");
String strBet = inScanner.nextLine();
int bet = Integer.parseInt(strBet);
while (bet < 0 || bet > currentPool)
{
System.out.print("Your bet MUST be between 0 and " + currentPool + " dollars.");
System.out.println("You have " + currentPool + " dollars.");
System.out.print("Enter an amount to bet (0 to quit): ");
strBet = (inScanner.nextLine());
bet = Integer.parseInt(strBet);
}
if (bet == 0)
{
System.out.println("You have " + currentPool + " dollars.");
System.out.println("Goodbye!");
return bet;
}
else
{
return bet;
}
}
private static char getHighLow(Scanner inScanner)
{
System.out.print("High, Low, or Sevens (H/L/S): ");
String hls = inScanner.nextLine();
char highLow = ' ';
if (hls.equals("H") || hls.equals("h"))
{
highLow = 'H';
}
else if (hls.equals("L") || hls.equals("l"))
{
highLow = 'L';
}
else if (hls.equals("S") || hls.equals("s"))
{
highLow = 'S';
}
else
{
System.out.print("ERROR: invalid character entered. Please try again.");
while (!hls.equals("H") || !hls.equals("h") || !hls.equals("L") || !hls.equals("l") || !hls.equals("S") || !hls.equals("s"))
{
System.out.println("High, Low, or Sevens (H/L/S): ");
hls = inScanner.nextLine();
}
}
return highLow;
}
private static int getRoll()
{
int roll = (int)Math.floor(Math.random() * 6 + 1);
return roll;
}
private static int determineWinnings(char highLow, int bet, int roll)
{
int rollOne = getRoll();
int rollTwo = getRoll();
int total = rollOne + rollTwo;
int winnings = bet + 0;
System.out.println("Die 1 rolls: " + rollOne);
System.out.println("Die 2 rolls: " + rollTwo);
System.out.println("Total of two dice is: " + total);
if (highLow == 'H')
{
if (total >= 8)
{
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = 0 - bet;
}
}
else if (highLow == 'L')
{
if (total <= 6)
{
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = (0 - bet);
}
}
else
{
if (total == 7)
{
winnings = bet * 4;
System.out.println("You won " + winnings + " dollars!");
}
else
{
System.out.println("You lost!");
winnings = 0 - bet;
}
}
return winnings;
}
}
答案 0 :(得分:1)
您不了解的部分是,每个方法-实际上,每个方法的每次调用-都有其自己的局部变量集合。这意味着
winnings
中声明的main
变量与在winnings
中声明的determineWinnings
变量不同; bet
中声明的main
变量与在bet
中声明的getBet
变量不同。您需要做的是确保将每个调用方法返回的值分配给要在调用方中存储的变量。方法。因此,在main
中,当您调用getBet
时,您实际上想写
bet = getBet(inScanner, currentPool);
,以便将从getBet
返回的值分配给bet
的{{1}}变量。同样,当您致电main
时,您需要写
determineWinnings
以便从winnings = determineWinnings(highLow, bet, roll);
返回的值被分配给determineWinnings
的{{1}}变量。
如果您不这样做,那么winnings
中的所有变量都将保留其原始值,main
的{{1}}和奖金的main
(因为100
只是编写currentPool
的另一种方式。这就是为什么您的最终值竟然是32
。
答案 1 :(得分:0)
为避免执行时跳过,请使用
bet = getBet(inScanner, currentPool);
highLow = getHighLow(inScanner);
winnings = determineWinnings(highLow, bet, roll);
代替直接调用
getBet(inScanner, currentPool);
getHighLow(inScanner);
determineWinnings(highLow, bet, roll);
跳过if语句和错误答案的原因:
getBet(inScanner,currentPool)方法返回未分配给
的值int bet =''; Java会尝试将''转换为32的整数。因此,在您每次调用getBet(inScanner,currentPool)时,即使输入的值不同(也会给出错误的答案),每次分配的赌注都是32。
如果getHighLow(inScanner)方法返回的是char值,但由于未将其分配给highLow,因此highLow始终具有值''。
由于highLow尚未分配实际值(H / L / S),因此if语句将被跳过为 ''!=(H / L / S)并始终执行else中的语句。