英文:
How to continue a loop with try catch?
问题
import java.util.ArrayList;
import java.util.Scanner;
public class McHouseCommand {
public static void main(String[] args) {
Scanner scanInt = new Scanner(System.in);
Scanner scanLine = new Scanner(System.in);
String mainMenu[] = {"McBurger", "McTrio", "Fries", "Nothing"};
String drinkMenu[] = {"Coca", "Pepsi", "Coffe", "Nothing"};
String sauceMenu[] = {"Ketchup", "Moutarde", "Mayo", "Nothing"};
int mainI = -1;
int drinkI = -1;
int sauceI = -1;
ArrayList<Integer> mainMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> drinkMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> sauceMenuChoice = new ArrayList<Integer>();
boolean mainRetake, drinkRetake, sauceRetake;
boolean mainWhilePass = false, drinkWhilePass = false, sauceWhilePass = false;
String mainRetakeAnswer, drinkRetakeAnswer, sauceRetakeAnswer;
System.out.println("Welcome to McHouse ! " + "\n");
/* Main Menu */
do {
mainI++;
do {
System.out.println("\nMain menu:\n");
for (int i = 0; i < mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": " + mainMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt());
if (mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
mainI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!mainWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
mainRetakeAnswer = scanLine.nextLine();
} while (!mainRetakeAnswer.equalsIgnoreCase("yes") && !mainRetakeAnswer.equalsIgnoreCase("no"));
if (mainRetakeAnswer.equalsIgnoreCase("yes")) {
mainRetake = true;
} else {
mainRetake = false;
}
} while (mainRetake);
System.out.println("");
/* Drink Menu */
do {
drinkI++;
do {
System.out.println("\nDrink menu:\n");
for (int i = 0; i < drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": " + drinkMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
drinkMenuChoice.add(scanInt.nextInt());
if (drinkMenuChoice.get(drinkI) > drinkMenu.length || drinkMenuChoice.get(drinkI) <= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
drinkI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!drinkWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase("yes") && !drinkRetakeAnswer.equalsIgnoreCase("no"));
if (drinkRetakeAnswer.equalsIgnoreCase("yes")) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println("");
/* Sauce Menu */
do {
sauceI++;
do {
System.out.println("\nSauce menu:\n");
for (int i = 0; i < sauceMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": " + sauceMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
sauceMenuChoice.add(scanInt.nextInt());
if (sauceMenuChoice.get(sauceI) > sauceMenu.length || sauceMenuChoice.get(sauceI) <= 0) {
sauceWhilePass = false;
sauceMenuChoice.remove(sauceI);
} else {
sauceWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
sauceI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!sauceWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
sauceRetakeAnswer = scanLine.nextLine();
} while (!sauceRetakeAnswer.equalsIgnoreCase("yes") && !sauceRetakeAnswer.equalsIgnoreCase("no"));
if (sauceRetakeAnswer.equalsIgnoreCase("yes")) {
sauceRetake = true;
} else {
sauceRetake = false;
}
} while (sauceRetake);
/* What did you buy */
System.out.println("");
System.out.println("Great!");
System.out.println("You purchased: \n");
System.out.println("Main Menu");
for (int i = 0; i < mainMenuChoice.size(); i++) {
int r = mainMenuChoice.get(i) - 1;
System.out.println((i + 1) + "| " + mainMenu[r]);
}
System.out.println("");
System.out.println("Drink");
for (int i = 0; i < drinkMenuChoice.size(); i++) {
int r = drinkMenuChoice.get(i) - 1;
System.out.println((i + 1) + "| " + drinkMenu[r]);
}
System.out.println("");
System.out.println("Sauce");
for (int i = 0; i < sauceMenuChoice.size(); i++) {
int r = sauceMenuChoice.get(i) - 1;
System.out.println((i + 1) + "| " + sauceMenu[r]);
}
/* Program end */
scanLine.close();
scanInt.close();
System.out.println("\n\nProgram end");
}
}
英文:
I've been trying for a few hours now, but I can't manage exceptions in loops while!
Basically I would like the loop to activate when there is an error!
Ps: it's a mini restaurant that I'm making to train on Java since I'm a beginner !
Here is where I would like to put the exceptions to redo it on the different parts of the code:
do {
System.out.println("\nMain menu:\n");
for (int i = 0; i < mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ mainMenu[i]);
}
try {
System.out.print("The number of your choice: ");
mainMenuChoice.add(scanInt.nextInt()); // exeption here
if(mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} catch (Exception e) {
System.err.println("Error: " + e);
}
} while (!mainWhilePass);
Here is full code:
import java.util.ArrayList;
import java.util.Scanner;
public class McHouseCommand {
public static void main(String[] args) {
Scanner scanInt = new Scanner(System.in);
Scanner scanLine = new Scanner(System.in);
String mainMenu[] = {"McBurger", "McTrio", "Fries" , "Nothing"};
String drinkMenu[] = {"Coca", "Pepsi", "Coffe", "Nothing"};
String sauceMenu[] = {"Ketchup", "Moutarde", "Mayo", "Nothing"};
int mainI = -1;
int drinkI = -1;
int sauceI = -1;
ArrayList<Integer> mainMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> drinkMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> sauceMenuChoice = new ArrayList<Integer>();
boolean mainRetake, drinkRetake, sauceRetake;
boolean mainWhilePass = false, drinkWhilePass = false, sauceWhilePass = false;
String mainRetakeAnswer, drinkRetakeAnswer, sauceRetakeAnswer;
System.out.println("Welcome to McHouse ! "+"\n");
/* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu */
do {
mainI ++;
do {
System.out.println("\nMain menu:\n");
for (int i = 0; i < mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ mainMenu[i]);
}
try {
System.out.print("The number of your choice: ");
mainMenuChoice.add(scanInt.nextInt()); // exeption here
if(mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} catch (Exception e) {
System.err.println("Error: " + e);
}
} while (!mainWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
mainRetakeAnswer = scanLine.nextLine();
} while (!mainRetakeAnswer.equalsIgnoreCase("yes") && !mainRetakeAnswer.equalsIgnoreCase("no"));
if (mainRetakeAnswer.equalsIgnoreCase("yes")) {
mainRetake = true;
} else {
mainRetake = false;
}
} while (mainRetake);
System.out.println("");
/* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu */
do {
drinkI ++;
do {
System.out.println("\nDrink menu:\n");
for (int i = 0; i < drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ drinkMenu[i]);
}
System.out.print("The number of your choice: ");
drinkMenuChoice.add(scanInt.nextInt());
if(drinkMenuChoice.get(drinkI) > drinkMenu.length || drinkMenuChoice.get(drinkI) <= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} while (!drinkWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase("yes") && !drinkRetakeAnswer.equalsIgnoreCase("no"));
if (drinkRetakeAnswer.equalsIgnoreCase("yes")) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println("");
/* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu */
do {
sauceI ++;
do {
System.out.println("\nSauce menu:\n");
for (int i = 0; i < sauceMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ sauceMenu[i]);
}
System.out.print("The number of your choice: ");
sauceMenuChoice.add(scanInt.nextInt());
if(sauceMenuChoice.get(sauceI) > sauceMenu.length || sauceMenuChoice.get(sauceI) <= 0) {
sauceWhilePass = false;
sauceMenuChoice.remove(sauceI);
} else {
sauceWhilePass = true;
}
} while (!sauceWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
sauceRetakeAnswer = scanLine.nextLine();
} while (!sauceRetakeAnswer.equalsIgnoreCase("yes") && !sauceRetakeAnswer.equalsIgnoreCase("no"));
if (sauceRetakeAnswer.equalsIgnoreCase("yes")) {
sauceRetake = true;
} else {
sauceRetake = false;
}
} while (sauceRetake);
/* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy */
System.out.println("");
System.out.println("Great! ");
System.out.println("You purchached: \n");
System.out.println("Main Menu");
for(int i = 0; i < mainMenuChoice.size();i++) {
int r = mainMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + "| "+ mainMenu[r]);
}
System.out.println("");
System.out.println("Drink");
for(int i = 0; i < drinkMenuChoice.size();i++) {
int r = drinkMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + "| "+ drinkMenu[r]);
}
System.out.println("");
System.out.println("Sauce");
for(int i = 0; i < sauceMenuChoice.size();i++) {
int r = sauceMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + "| "+ sauceMenu[r]);
}
/* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end */
scanLine.close();
scanInt.close();
System.out.println("\n\nProgarm end");
}
}
Here is the final code :
import java.util.Scanner;
public class McHouseCommand {
public static void main(String[] args) {
Scanner scanInt = new Scanner(System.in);
Scanner scanLine = new Scanner(System.in);
String mainMenu[] = {"McBurger", "McTrio", "Fries" , "Nothing"};
String drinkMenu[] = {"Coca", "Pepsi", "Coffe", "Nothing"};
String sauceMenu[] = {"Ketchup", "Moutarde", "Mayo", "Nothing"};
int mainI = -1;
int drinkI = -1;
int sauceI = -1;
ArrayList<Integer> mainMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> drinkMenuChoice = new ArrayList<Integer>();
ArrayList<Integer> sauceMenuChoice = new ArrayList<Integer>();
boolean mainRetake, drinkRetake, sauceRetake;
boolean mainWhilePass = false, drinkWhilePass = false, sauceWhilePass = false;
String mainRetakeAnswer, drinkRetakeAnswer, sauceRetakeAnswer;
System.out.println("Welcome to McHouse ! "+"\n");
/* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu */
do {
mainI ++;
do {
System.out.println("\nMain menu:\n");
for (int i = 0; i < mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ mainMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt()); // exeption here
if(mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
mainI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!mainWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
mainRetakeAnswer = scanLine.nextLine();
} while (!mainRetakeAnswer.equalsIgnoreCase("yes") && !mainRetakeAnswer.equalsIgnoreCase("no"));
if (mainRetakeAnswer.equalsIgnoreCase("yes")) {
mainRetake = true;
} else {
mainRetake = false;
}
} while (mainRetake);
System.out.println("");
/* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu */
do {
drinkI ++;
do {
System.out.println("\nDrink menu:\n");
for (int i = 0; i < drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ drinkMenu[i]);
}
System.out.print("The number of your choice: ");
drinkMenuChoice.add(scanInt.nextInt());
if(drinkMenuChoice.get(drinkI) > drinkMenu.length || drinkMenuChoice.get(drinkI) <= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} while (!drinkWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase("yes") && !drinkRetakeAnswer.equalsIgnoreCase("no"));
if (drinkRetakeAnswer.equalsIgnoreCase("yes")) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println("");
/* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu */
do {
drinkI ++;
do {
System.out.println("\nDrink menu:\n");
for (int i = 0; i < drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ drinkMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
drinkMenuChoice.add(scanInt.nextInt()); // exeption here
if(drinkMenuChoice.get(drinkI) > drinkMenu.length || drinkMenuChoice.get(drinkI) <= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
drinkI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!drinkWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase("yes") && !drinkRetakeAnswer.equalsIgnoreCase("no"));
if (drinkRetakeAnswer.equalsIgnoreCase("yes")) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println("");
/* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy *//* What did you buy */
do {
sauceI ++;
do {
System.out.println("\nSauce menu:\n");
for (int i = 0; i < sauceMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + ": "+ sauceMenu[i]);
}
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
sauceMenuChoice.add(scanInt.nextInt()); // exeption here
if(sauceMenuChoice.get(sauceI) > sauceMenu.length || sauceMenuChoice.get(sauceI) <= 0) {
sauceWhilePass = false;
sauceMenuChoice.remove(sauceI);
} else {
sauceWhilePass = true;
}
} else {
System.err.print("Not valid choice: " + scanInt.next());
sauceI -= 1;
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
} while (!sauceWhilePass);
do {
System.out.print("\n[Yes | No] Do you want to retake: ");
sauceRetakeAnswer = scanLine.nextLine();
} while (!sauceRetakeAnswer.equalsIgnoreCase("yes") && !sauceRetakeAnswer.equalsIgnoreCase("no"));
if (sauceRetakeAnswer.equalsIgnoreCase("yes")) {
sauceRetake = true;
} else {
sauceRetake = false;
}
} while (sauceRetake);
System.out.println("");
/* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end */
scanLine.close();
scanInt.close();
System.out.println("\n\nProgarm end");
}
}
答案1
得分: 0
你必须在读取扫描器之前测试类型,即使出现错误,也必须读取输入:
try {
System.out.print("您的选择数字:");
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt()); // 这里出现异常
if (mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.out.print("无效选择:" + scanInt.next());
}
} catch (Exception e) {
System.err.println("错误:" + e);
scanInt.next();
}
英文:
You must test the type before reading the scanner and even if there is an error, you must read the input :
try {
System.out.print("The number of your choice: ");
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt()); // exeption here
if(mainMenuChoice.get(mainI) > mainMenu.length || mainMenuChoice.get(mainI) <= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.out.print("not valid choice: " + scanInt.next());
}
} catch (Exception e) {
System.err.println("Error: " + e);
scanInt.next();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论