Java 为什么我的代码打印的总数是错误的?
为什么当你赔钱时,我的代码会将损失金额替换为总金额?我不确定错误是在某个函数中还是在主函数中 替换错误的输出示例:Java 为什么我的代码打印的总数是错误的?,java,Java,为什么当你赔钱时,我的代码会将损失金额替换为总金额?我不确定错误是在某个函数中还是在主函数中 替换错误的输出示例: You have 100 dollars. Enter an amount to bet (0 to quit):56 High, low or sevens (H/L/S):l Die 1 rolls: 4 Die 2 rolls: 5 Total of two dice is: 9 You lost! You have 56 dollars. Enter an amount t
You have 100 dollars.
Enter an amount to bet (0 to quit):56
High, low or sevens (H/L/S):l
Die 1 rolls: 4
Die 2 rolls: 5
Total of two dice is: 9
You lost!
You have 56 dollars.
Enter an amount to bet (0 to quit):
这是我的密码:
import java.util.Scanner;
public class Project07 {
public static void main(String[] args) {
// Fill in the body
int totalAmount = 100;
while(totalAmount>0){
boolean b = true;
boolean c = true;
int dollars = 100;
Scanner inScanner = new Scanner(System.in);
System.out.println("You have "+totalAmount+" dollars.");
while (b){
dollars = getBet(inScanner, dollars);
if(dollars>0 && dollars<100){
b=false;
}
}
char sevensHL = getHighLow(inScanner);
while(sevensHL == 'q'){
System.out.println("ERROR! Enter H,L, or S.");
sevensHL = getHighLow(inScanner);
}
int die1 = getRoll();
int die2 = getRoll();
System.out.println("Die 1 rolls: " + die1);
System.out.println("Die 2 rolls: " + die2);
int total = die1 + die2;
System.out.println("Total of two dice is: " + total);
int winnings = determineWinnings(sevensHL, dollars, total);
totalAmount = totalAmount + winnings;
}
System.out.println("You have 0 dollars left");
System.out.print("Goodbye!");
}
// Given a Scanner and a current maximum amount of money, prompt the user for
// an integer representing the number of dollars that they want to bet. This
// number must be between 0 and to maximum number of dollars. If the user enters
// a number that is out of bounds, display an error message and ask again.
// Return the bet to the calling program.
private static int getBet(Scanner inScanner, int currentPool) {
// Fill in the body
int wager;
System.out.print("Enter an amount to bet (0 to quit):");
wager = inScanner.nextInt();
if(wager == 0){
System.out.println("You have "+ currentPool +" left");
System.out.print("Goodbye!");
System.exit(0);
return 0;
}
else if(wager<0 || wager>100){
System.out.println("Your bet MUST be between 0 and 100 dollars");
wager = 0;
int currentDollars=currentPool - wager;
return currentDollars;
}
else{
int currentDollars=currentPool - wager;
return currentDollars;
}
}
// Given a Scanner, prompt the user for a single character indicating whether they
// would like to bet High ('H'), Low ('L') or Sevens ('S'). Your code should accept
// either capital or lowercase answers, but should display an error if the user attempts
// to enter anything but one of these 3 values and prompt for a valid answer.
// Return the character to the calling program.
private static char getHighLow(Scanner inScanner) {
// Fill in the body
System.out.print("High, low or sevens (H/L/S):");
String input = inScanner.next();
if(input.equalsIgnoreCase("h")){
char out = 'h';
return out;
}
else if(input.equalsIgnoreCase("l")){
char out = 'l';
return out;
}
else if(input.equalsIgnoreCase("s")){
char out = 's';
return out;
}
else{
return 'q';
}
}
// Produce a random roll of a single six-sided die and return that value to the calling
// program
private static int getRoll() {
// Fill in the body
int die = 1 + (int)(Math.random() * ((6 - 1) + 1));
return die;
}
// Given the choice of high, low or sevens, the player's bet and the total result of
// the roll of the dice, determine how much the player has won. If the player loses
// the bet then winnings should be negative. If the player wins, the winnings should
// be equal to the bet if the choice is High or Low and 4 times the bet if the choice
// was Sevens. Return the winnings to the calling program.
private static int determineWinnings(char highLow, int bet, int roll) {
// Fill in the body
if (highLow == 'h' && roll >=8){
int payout = bet;
System.out.println("You won " + payout +" dollars!");
return payout;
}
else if(highLow == 'l' && roll <= 6){
int payout = bet;
System.out.println("You won " + payout +" dollars!");
return payout;
}
else if(highLow == 's' && roll == 7){
int payout = 4*bet;
System.out.println("You won " + payout +" dollars!");
return payout;
}
else{
System.out.println("You lost!");
int payout = (-1)*bet;
return payout;
}
}
}
import java.util.Scanner;
公开课项目07{
公共静态void main(字符串[]args){
//填塞
整数总额=100;
而(总金额>0){
布尔b=真;
布尔c=真;
整数美元=100;
Scanner inScanner=新扫描仪(System.in);
System.out.println(“您有”+totalAmount+“美元”);
而(二){
美元=getBet(inScanner,美元);
如果($s>0&&$s我们似乎在getBet()
方法本身中从剩余金额中扣除了赌注。这与赢/输的相加一起会导致不正确的金额
我们需要更改getBet()
方法,如下所示:
private static int getBet(Scanner inScanner, int currentPool) {
// Fill in the body
int wager;
System.out.print("Enter an amount to bet (0 to quit):");
wager = inScanner.nextInt();
if(wager == 0){
System.out.println("You have "+ currentPool +" left");
System.out.print("Goodbye!");
System.exit(0);
return 0;
}
else if(wager<0 || wager>100){
System.out.println("Your bet MUST be between 0 and 100 dollars");
wager = 0;
return wager;
}
else{
return wager;
}
}
private static int getBet(扫描程序inScanner,int currentPool){
//填塞
国际赌注;
系统输出打印(“输入下注金额(0退出):”;
下注=inScanner.nextInt();
如果(下注==0){
System.out.println(“您有”+currentPool+“left”);
系统输出打印(“再见!”);
系统出口(0);
返回0;
}
否则,如果(下注100){
System.out.println(“您的赌注必须在0到100美元之间”);
赌注=0;
回注;
}
否则{
回注;
}
}
另外,我们需要传递totalDollars
,而不是在getBet()调用中传递dollars
,因为这是正确的金额。因此,更改:
dollars=getBet(inScanner,dollars);
到
dollars=getBet(inScanner,totalAmount);
在进行这些更改后,它应该显示正确的总数。您是否在调试器中逐步检查了代码?我们希望他在来这里之前会这样做,对吗?因为getBet()
是错误的