我还是Java的新手,我有一个实验室,需要模拟一个彩票游戏,生成一个介于1-10之间的数字。它首先询问用户他们想要购买多少张彩票,然后询问他们是否希望计算机为他们生成猜测,如果是,则它将生成猜测并显示中奖号码。如果用户说否,那么用户将自己输入猜测,并显示中奖号码。
我遇到了一个问题,当有人输入yes或no时,如何编写代码。我应该做一个do while循环吗?
下面是我现在拥有的代码。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double TICKET_PRICE = 2.00;
System.out.println("Welcome to the State of Florida Play10 Lottery Game. Ticket Price: $" + TICKET_PRICE);
System.out.println("How many tickets would you like to purchase?");
int ticketsPurchased = input.nextInt();
System.out.print("Please enter " + (ticketsPurchased) + " to confirm your credit carde charge: ");
int creditCardCharge = input.nextInt();
if (ticketsPurchased != creditCardCharge) {
System.out.println("Wrong number, please enter again: ");
return;
}
if (ticketsPurchased == creditCardCharge) {
System.out.println("Thank you. Your credit card will be charged $" + (ticketsPurchased * 2));
}
int min = 1;
int max = 10;
int winner;
winner = min + (int)(Math.random() * ((max - min) + 1));
System.out.print("Would you like the computer to generate your guesses? Enter 'Y' or 'N': ");
String computerGeneratedGuess = input.nextLine();
int guess = 0;
int winCtr = 0;
String output = "";
}
算法如下: 1.获取需要购买的门票数量,计算并确认信用卡费用。2.生成随机中奖整数,生成随机猜测或提示用户猜测。3.上报中奖号码、中奖彩票、总中奖金额、总损失、允许扣除额
这就是实验室本身:Lab05 Lottery game
发布于 2019-09-22 23:42:32
通常,布尔值可以方便地控制这样的循环。类似于:
boolean gameOver = false;
int theGuess = 0;
while (!gameOver) {
if (computerGeneratedGuess == 'Y') {
theGuess = //code to generate a random number
}
else {
theGuess = //code to for user to enter a guess
}
if (theGuess == winner) {
gameOver = true;
}
https://stackoverflow.com/questions/58054074
复制