如何根据 java 程序中的用户输入继续循环
How to continue loop based on user input in java program
我有一个彩票程序,可以 运行 投注并随机生成号码。当用户的总奖金达到 5000 美元时,该用户将可以选择兑现 5000 美元并使用剩余余额继续循环。如果选择不兑现,用户也可以结束循环。如何根据用户输入继续循环?如果用户输入是,循环将继续,但我不知道该怎么做。有人可以帮忙吗?如果循环继续下去,由于兑现而从用户的总赢款中扣除 5000 美元,剩余余额将用于玩游戏。
我试过在主循环的多个地方放置用户输入的if语句,但用户预算一直在增加,并没有减去5000美元。
java.util.Scanner
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win ,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win ,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win ,000");
totalWin += 100;
} else
System.out.println("Sorry, no match");
budget += totalWin;
totalWin=0;
if (budget > initBudget)
if ((budget-initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
System.out.println("Budget After play " + budget);
}
所以在我的代码中,我有循环
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
此循环应接收用户输入,然后从总赢额中减去 5000,并使用剩余预算继续游戏。但它只会增加。如果我有 -5000,不明白为什么它会增加。
(如果可能,请帮我把答案改成 yes/no 而不是输入 1)
我假设您使用的是 java.util.Scanner
。答案在这里;
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you " +
"would like to stop");
String decison = input.nextLine();
if ("yes".equals(decison))
{
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // break the statement.
Erçin Akçay 解释了如何将输入从 int 更改为 String。
我查看了您的代码并使用它制作了一个可运行的主要方法,它按您预期的那样工作。也许初始预算需要 5k 的事实令人困惑?
if ((budget - initBudget) >= 5000)
这是我用来检查你的代码是否有帮助的全部class:
import java.util.Scanner;
public class Test {
private static int budget;
private static int lottery;
private static int guess;
private static int totalWin;
private static int initBudget = 4500;
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
budget = initBudget;
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win ,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win ,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win ,000");
totalWin += 100;
} else {
System.out.println("Sorry, no match");
}
budget += totalWin;
totalWin = 0;
if (budget > initBudget) {
if ((budget - initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println(
"Enter 1 if you would like to cash out and use remaining balance to play and 0 if you "
+ "would like to stop");
int decison = input.nextInt();
if (decison == 1) {
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break;
}
}
System.out.println("Budget After play " + budget);
}
}
}
您可以这样更改代码:
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // breaking the loop (if user enters anything other than yes)
这里我使用了String
(将用户的回复存储为一个词)。如果他输入Yes
、yes
或YES
(忽略大小写,则认为用户要提现)循环不会中断。
我有一个彩票程序,可以 运行 投注并随机生成号码。当用户的总奖金达到 5000 美元时,该用户将可以选择兑现 5000 美元并使用剩余余额继续循环。如果选择不兑现,用户也可以结束循环。如何根据用户输入继续循环?如果用户输入是,循环将继续,但我不知道该怎么做。有人可以帮忙吗?如果循环继续下去,由于兑现而从用户的总赢款中扣除 5000 美元,剩余余额将用于玩游戏。
我试过在主循环的多个地方放置用户输入的if语句,但用户预算一直在增加,并没有减去5000美元。
java.util.Scanner
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win ,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win ,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win ,000");
totalWin += 100;
} else
System.out.println("Sorry, no match");
budget += totalWin;
totalWin=0;
if (budget > initBudget)
if ((budget-initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
System.out.println("Budget After play " + budget);
}
所以在我的代码中,我有循环
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
此循环应接收用户输入,然后从总赢额中减去 5000,并使用剩余预算继续游戏。但它只会增加。如果我有 -5000,不明白为什么它会增加。
(如果可能,请帮我把答案改成 yes/no 而不是输入 1)
我假设您使用的是 java.util.Scanner
。答案在这里;
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you " +
"would like to stop");
String decison = input.nextLine();
if ("yes".equals(decison))
{
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // break the statement.
Erçin Akçay 解释了如何将输入从 int 更改为 String。
我查看了您的代码并使用它制作了一个可运行的主要方法,它按您预期的那样工作。也许初始预算需要 5k 的事实令人困惑?
if ((budget - initBudget) >= 5000)
这是我用来检查你的代码是否有帮助的全部class:
import java.util.Scanner;
public class Test {
private static int budget;
private static int lottery;
private static int guess;
private static int totalWin;
private static int initBudget = 4500;
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
budget = initBudget;
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win ,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win ,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win ,000");
totalWin += 100;
} else {
System.out.println("Sorry, no match");
}
budget += totalWin;
totalWin = 0;
if (budget > initBudget) {
if ((budget - initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println(
"Enter 1 if you would like to cash out and use remaining balance to play and 0 if you "
+ "would like to stop");
int decison = input.nextInt();
if (decison == 1) {
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break;
}
}
System.out.println("Budget After play " + budget);
}
}
}
您可以这样更改代码:
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // breaking the loop (if user enters anything other than yes)
这里我使用了String
(将用户的回复存储为一个词)。如果他输入Yes
、yes
或YES
(忽略大小写,则认为用户要提现)循环不会中断。