我目前正在使用Java进行初学者编程课程中的项目。该项目涉及制造自动售货机。从本质上讲,自动售货机会询问您想要什么物品,然后会询问您的钱。每次有人选择某个项目时,该项目的数量都会减少一。我想要做的是使某物品缺货(数量为0)时,代码返回到开头,并再次询问您想要什么物品。我也想这样做,所以如果有人输入了无效的数字,代码也会重新启动。我该怎么做?这是代码。
import java.util.Scanner;
public class VendingMachine {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int potatoChips = 0;
int cookies = 3;
int candies = 4;
int quarters;
int dimes;
int nickels;
System.out.println("Select the number for the item you would like");
System.out.println("For Potato Chips, Enter 1");
System.out.println("For Cookies, Enter 2");
System.out.println("For Candies, Enter 3");
int itemSelection = user_input.nextInt();
if (itemSelection == 1) {
if (potatoChips > 0) {
potatoChips = potatoChips - 1;
System.out.println("You chose potato chips.");
System.out.println("That will be $1.25");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 125) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 125 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 125);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
if (itemSelection == 2) {
if (cookies > 0) {
cookies = cookies - 1;
System.out.println("You chose a cookie.");
System.out.println("That will be $0.85");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 85) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 85 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 85);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
if (itemSelection == 3) {
if (candies > 0) {
candies = candies - 1;
System.out.println("You chose a candy.");
System.out.println("That will be $0.95");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 95) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 95 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 95);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
else {
System.out.println("This is not an item. Please select another item.");
}
}
}
答案 0 :(得分:0)
您可以使用while
循环来实现此目的。
考虑通过以下方式构建代码:
Scanner user_input = new Scanner(System.in);
// This will be used to track if the vending maching is finished or not
boolean isFinished = false;
// Use a while loop, which causes the program to repeat until the variable
// isFinished is true (see below)
while(isFinished == false) {
int potatoChips = 0;
int cookies = 3;
int candies = 4;
int quarters;
int dimes;
int nickels;
System.out.println("Select the number for the item you would like");
System.out.println("For Potato Chips, Enter 1");
System.out.println("For Cookies, Enter 2");
System.out.println("For Candies, Enter 3");
int itemSelection = user_input.nextInt();
if (itemSelection == 1) {
if (potatoChips > 0) {
potatoChips = potatoChips - 1;
System.out.println("You chose potato chips.");
System.out.println("That will be $1.25");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 125) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 125 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 125);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
// This causes the machine loop to end
isFinished = true;
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
if (itemSelection == 2) {
if (cookies > 0) {
cookies = cookies - 1;
System.out.println("You chose a cookie.");
System.out.println("That will be $0.85");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 85) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 85 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 85);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
// This causes the machine loop to end
isFinished = true;
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
if (itemSelection == 3) {
if (candies > 0) {
candies = candies - 1;
System.out.println("You chose a candy.");
System.out.println("That will be $0.95");
System.out.println("How many quarters do you have?");
quarters = user_input.nextInt();
System.out.println("How many dimes do you have?");
dimes = user_input.nextInt();
System.out.println("How many nickels do you have?");
nickels = user_input.nextInt();
int nickelsToPennies = (nickels * 5);
int dimesToPennies = (dimes * 10);
int quartersToPennies = (quarters * 25);
int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);
if (pennies < 95) {
System.out.println("You have not entered enough money. Have a great day.");
}
else if (pennies == 95 ) {
System.out.println("Here is your snack. Have a great day");
}
else {
double changeInPennies = (pennies - 95);
double change = (changeInPennies / 100);
System.out.println("Your change is $" + change + ". Have a great day.");
}
// This causes the machine loop to end
isFinished = true;
}
else {
System.out.println("This item is out of stock. Please select another item.");
}
}
else {
System.out.println("This is not an item. Please select another item.");
}
}
答案 1 :(得分:0)
您必须使用连续的while
循环。像这样:
while(true) {
//collect user input
//process
}
这样,当处理完您的用户请求后,它就会循环回到开头。如果您想在完成之前将其停止,例如输入错误,则可以使用continue
关键字来停止当前循环并返回到开头。 break
将使您摆脱整个while循环并结束代码。
答案 2 :(得分:0)
我做了一个小例子。基本概念是您可以进行while循环。您可以添加一些内容,例如用于打破循环的附加条件,这可能需要防止无限循环。但是基本概念是,这个迷你示例是在每次有人选择项目时明显减少其项目。如果他们这样做,我们将从该商品的数量中减去1。然后,如果该项等于0,我们将使用CONTINUE语句重新开始循环并要求他们再次选择。我认为您想要的是继续声明。这样一来,您就可以重新开始循环,而不会脱离循环。它与break语句相似,不同之处在于,如果特定条件为“ true”,它不会完全退出循环,而是会再次重新开始循环。当此示例中的1个或两个项目的数量为0时,我们的条件为true:
public class ChapterOneBasics {
public static void main(String[] args) {
int butterfinger = 5;
int bQuantity = 3;
int snickers = 4;
int sQuantity = 3;
Scanner reader = new Scanner(System.in);
while(true){
System.out.println("Please select an item (5 for butter finger, 4 for snickers)");
int item = reader.nextInt();
if((item == 5 && bQuantity == 0) || (item == 4 && sQuantity == 0)) {
System.out.println("We are out of that item!");
continue;
}
else {
if(item == 5) {
System.out.println("Here's your butter finger");
bQuantity--;
}
else if(item == 4) {
System.out.println("Here is your snickers");
sQuantity--;
}
else if(item != 5 || item != 4){
System.out.println("Enter a valid item");
continue;
}
}
}
}
}
这是您的输出:
Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
We are out of that item!
Please select an item (5 for butter finger, 4 for snickers)
如您所见,当他们选择我们不在的项目时,continue语句会强制循环重新开始。