[英]How can I make my methods restart my after being executed once?
How can I alter my code to make it so that it reruns the if/else-if statements after being run once. 如何更改我的代码以使其在运行一次后重新运行if / else-if语句。
Eg How can I go: top menu>prompt for input(which is 2)>submenu>prompt for input(1 or2)>add/delete>back to top menu/subMenu 例如我该怎么做:顶部菜单>提示输入(2)>子菜单>提示输入(1或2)>添加/删除>返回顶部菜单/子菜单
/**
* A simple program of a grocery store, which assists
* the purchases, calculate total price and display bill.
**/
public class GroceryStore {
// this method manages the entire shopping process
public void start() {
final String UPI = "wcor690"; // a constant for student UPI
Stock stock = new Stock();
stock.loadItems("stock.txt");
Cart cart = new Cart();
System.out.println("==============================================================");
System.out.println("------This is a simple grocery store program by " + UPI + ".------");
topMenu();
int input = getChoice(1, 3);
if (input == 1){
stock.displayItems();
topMenu();
}
else if (input == 2){
subMenu();
input = getChoice(1, 4);
if (input == 1){
String itemCode = Keyboard.readInput();
cart.addItem(stock.findItem(itemCode));
topMenu();
}
else if (input == 2){
String itemCode = Keyboard.readInput();
cart.deleteItem(itemCode);
subMenu();
}
else if (input == 3){
cart.checkOut();
}
else if (input == 4){
cart.deleteAll();
System.out.println("All items are cleared from the shopping cart");
topMenu();
}
}
else if (input == 3){
System.out.println("Exit");
}
stock.saveItems("stock2.txt");
System.out.println("--------------------------------------------------------------");
System.out.println("---------------Thank you for shopping with us!----------------");
System.out.println("==============================================================");
}
// this method displays the top-level menu
private void topMenu() {
System.out.println("1. Show items");
System.out.println("2. Start shopping online");
System.out.println("3. Exit");
}
// this method displays the second-level menu
private void subMenu() {
System.out.println("1. Add item");
System.out.println("2. Remove item");
System.out.println("3. Checkout");
System.out.println("4. Exit without buying");
}
// this method gets the user's input choice
private int getChoice(int lower, int upper) {
String userInput = Keyboard.readInput();
int input = Integer.parseInt(userInput);
while (input<lower || input>upper){
System.out.println("Invalid choice, please enter a valid choice");
input = Integer.parseInt(Keyboard.readInput());
}
return input;
}
}
You can use some sort of loop, which will continue to excecute the code until a condition is met. 您可以使用某种循环,这将继续执行代码,直到满足条件。
Check out this link . 看看这个链接 。
Here is an example: 这是一个例子:
do {
code line 1;
code line 2;
code line 3;
...
} while(yourCondition);
If yourCondition
is satisfied ( yourCondition == true
), the code will go back to code line 1
(will perform the code block between do
and while
) and it'll stop once the condition isn't satisfied( yourCondition == false
). 如果你的yourCondition
得到满足( yourCondition == true
),代码将返回到code line 1
(将执行do
和while
之间的代码块),并且一旦条件不满足就会停止( yourCondition == false
)。
Now, after you understand how it works, you can easily apply this to your code. 现在,在了解它的工作原理后,您可以轻松地将其应用于您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.