如何使用 try catch 继续循环?

huangapple go评论76阅读模式
英文:

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(&quot;\nMain menu:\n&quot;);
for (int i = 0; i &lt; mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ mainMenu[i]);
}
try {
System.out.print(&quot;The number of your choice: &quot;);
mainMenuChoice.add(scanInt.nextInt());		// exeption here 	
if(mainMenuChoice.get(mainI) &gt; mainMenu.length || mainMenuChoice.get(mainI) &lt;= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + 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[] = {&quot;McBurger&quot;, &quot;McTrio&quot;, &quot;Fries&quot; , &quot;Nothing&quot;};
String drinkMenu[] = {&quot;Coca&quot;, &quot;Pepsi&quot;, &quot;Coffe&quot;, &quot;Nothing&quot;};
String sauceMenu[] = {&quot;Ketchup&quot;, &quot;Moutarde&quot;, &quot;Mayo&quot;, &quot;Nothing&quot;};
int mainI = -1;
int drinkI = -1; 
int sauceI = -1;
ArrayList&lt;Integer&gt; mainMenuChoice = new ArrayList&lt;Integer&gt;(); 
ArrayList&lt;Integer&gt; drinkMenuChoice = new ArrayList&lt;Integer&gt;();
ArrayList&lt;Integer&gt; sauceMenuChoice = new ArrayList&lt;Integer&gt;();
boolean mainRetake, drinkRetake, sauceRetake;
boolean mainWhilePass = false, drinkWhilePass = false, sauceWhilePass = false;
String mainRetakeAnswer, drinkRetakeAnswer, sauceRetakeAnswer;
System.out.println(&quot;Welcome to McHouse ! &quot;+&quot;\n&quot;);
/* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu */
do {
mainI ++;
do {
System.out.println(&quot;\nMain menu:\n&quot;);
for (int i = 0; i &lt; mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ mainMenu[i]);
}
try {
System.out.print(&quot;The number of your choice: &quot;);
mainMenuChoice.add(scanInt.nextInt());		// exeption here 	
if(mainMenuChoice.get(mainI) &gt; mainMenu.length || mainMenuChoice.get(mainI) &lt;= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + e);
}
} while (!mainWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
mainRetakeAnswer = scanLine.nextLine();
} while (!mainRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !mainRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (mainRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
mainRetake = true;
} else {
mainRetake = false;
}
} while (mainRetake);
System.out.println(&quot;&quot;);
/* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu */
do {
drinkI ++;
do {
System.out.println(&quot;\nDrink menu:\n&quot;);
for (int i = 0; i &lt; drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ drinkMenu[i]);
}
System.out.print(&quot;The number of your choice: &quot;);
drinkMenuChoice.add(scanInt.nextInt());
if(drinkMenuChoice.get(drinkI) &gt; drinkMenu.length || drinkMenuChoice.get(drinkI) &lt;= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} while (!drinkWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !drinkRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println(&quot;&quot;);
/* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu */
do {
sauceI ++;
do {
System.out.println(&quot;\nSauce menu:\n&quot;);
for (int i = 0; i &lt; sauceMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ sauceMenu[i]);
}
System.out.print(&quot;The number of your choice: &quot;);
sauceMenuChoice.add(scanInt.nextInt());
if(sauceMenuChoice.get(sauceI) &gt; sauceMenu.length || sauceMenuChoice.get(sauceI) &lt;= 0) {
sauceWhilePass = false;
sauceMenuChoice.remove(sauceI);
} else {
sauceWhilePass = true;
}
} while (!sauceWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
sauceRetakeAnswer = scanLine.nextLine();
} while (!sauceRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !sauceRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (sauceRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
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(&quot;&quot;);
System.out.println(&quot;Great! &quot;);
System.out.println(&quot;You purchached: \n&quot;);
System.out.println(&quot;Main Menu&quot;);
for(int i = 0; i &lt; mainMenuChoice.size();i++) {
int r = mainMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + &quot;| &quot;+ mainMenu[r]);
}
System.out.println(&quot;&quot;);
System.out.println(&quot;Drink&quot;);
for(int i = 0; i &lt; drinkMenuChoice.size();i++) {
int r = drinkMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + &quot;| &quot;+ drinkMenu[r]);
}
System.out.println(&quot;&quot;);
System.out.println(&quot;Sauce&quot;);
for(int i = 0; i &lt; sauceMenuChoice.size();i++) {
int r = sauceMenuChoice.get(i) - 1;
System.out.println( (i+ 1) + &quot;| &quot;+ sauceMenu[r]);
}
/* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end */
scanLine.close();
scanInt.close();
System.out.println(&quot;\n\nProgarm end&quot;);
}
}

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[] = {&quot;McBurger&quot;, &quot;McTrio&quot;, &quot;Fries&quot; , &quot;Nothing&quot;};
String drinkMenu[] = {&quot;Coca&quot;, &quot;Pepsi&quot;, &quot;Coffe&quot;, &quot;Nothing&quot;};
String sauceMenu[] = {&quot;Ketchup&quot;, &quot;Moutarde&quot;, &quot;Mayo&quot;, &quot;Nothing&quot;};
int mainI = -1;
int drinkI = -1; 
int sauceI = -1;
ArrayList&lt;Integer&gt; mainMenuChoice = new ArrayList&lt;Integer&gt;(); 
ArrayList&lt;Integer&gt; drinkMenuChoice = new ArrayList&lt;Integer&gt;();
ArrayList&lt;Integer&gt; sauceMenuChoice = new ArrayList&lt;Integer&gt;();
boolean mainRetake, drinkRetake, sauceRetake;
boolean mainWhilePass = false, drinkWhilePass = false, sauceWhilePass = false;
String mainRetakeAnswer, drinkRetakeAnswer, sauceRetakeAnswer;
System.out.println(&quot;Welcome to McHouse ! &quot;+&quot;\n&quot;);
/* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu *//* Main Menu */
do {
mainI ++;
do {
System.out.println(&quot;\nMain menu:\n&quot;);
for (int i = 0; i &lt; mainMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ mainMenu[i]);
}
try {
System.out.print(&quot;The number of your choice: &quot;);
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt());      // exeption here
if(mainMenuChoice.get(mainI) &gt; mainMenu.length || mainMenuChoice.get(mainI) &lt;= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.err.print(&quot;Not valid choice: &quot; + scanInt.next());
mainI -= 1;
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + e);
scanInt.next();
}
} while (!mainWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
mainRetakeAnswer = scanLine.nextLine();
} while (!mainRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !mainRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (mainRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
mainRetake = true;
} else {
mainRetake = false;
}
} while (mainRetake);
System.out.println(&quot;&quot;);
/* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu *//* Drink Menu */
do {
drinkI ++;
do {
System.out.println(&quot;\nDrink menu:\n&quot;);
for (int i = 0; i &lt; drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ drinkMenu[i]);
}
System.out.print(&quot;The number of your choice: &quot;);
drinkMenuChoice.add(scanInt.nextInt());
if(drinkMenuChoice.get(drinkI) &gt; drinkMenu.length || drinkMenuChoice.get(drinkI) &lt;= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} while (!drinkWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !drinkRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println(&quot;&quot;);
/* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu *//* Sauce Menu */
do {
drinkI ++;
do {
System.out.println(&quot;\nDrink menu:\n&quot;);
for (int i = 0; i &lt; drinkMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ drinkMenu[i]);
}
try {
System.out.print(&quot;The number of your choice: &quot;);
if (scanInt.hasNextInt()) {
drinkMenuChoice.add(scanInt.nextInt());      // exeption here
if(drinkMenuChoice.get(drinkI) &gt; drinkMenu.length || drinkMenuChoice.get(drinkI) &lt;= 0) {
drinkWhilePass = false;
drinkMenuChoice.remove(drinkI);
} else {
drinkWhilePass = true;
}
} else {
System.err.print(&quot;Not valid choice: &quot; + scanInt.next());
drinkI -= 1;
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + e);
scanInt.next();
}
} while (!drinkWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
drinkRetakeAnswer = scanLine.nextLine();
} while (!drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !drinkRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (drinkRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
drinkRetake = true;
} else {
drinkRetake = false;
}
} while (drinkRetake);
System.out.println(&quot;&quot;);
/* 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(&quot;\nSauce menu:\n&quot;);
for (int i = 0; i &lt; sauceMenu.length; i++) {
int menuNumber = i + 1;
System.out.println(menuNumber + &quot;: &quot;+ sauceMenu[i]);
}
try {
System.out.print(&quot;The number of your choice: &quot;);
if (scanInt.hasNextInt()) {
sauceMenuChoice.add(scanInt.nextInt());      // exeption here
if(sauceMenuChoice.get(sauceI) &gt; sauceMenu.length || sauceMenuChoice.get(sauceI) &lt;= 0) {
sauceWhilePass = false;
sauceMenuChoice.remove(sauceI);
} else {
sauceWhilePass = true;
}
} else {
System.err.print(&quot;Not valid choice: &quot; + scanInt.next());
sauceI -= 1;
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + e);
scanInt.next();
}
} while (!sauceWhilePass);
do {
System.out.print(&quot;\n[Yes | No] Do you want to retake: &quot;);
sauceRetakeAnswer = scanLine.nextLine();
} while (!sauceRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !sauceRetakeAnswer.equalsIgnoreCase(&quot;no&quot;));
if (sauceRetakeAnswer.equalsIgnoreCase(&quot;yes&quot;)) {
sauceRetake = true;
} else {
sauceRetake = false;
}
} while (sauceRetake);
System.out.println(&quot;&quot;);
/* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end *//* Program end */
scanLine.close();
scanInt.close();
System.out.println(&quot;\n\nProgarm end&quot;);
}
}

答案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(&quot;The number of your choice: &quot;);
if (scanInt.hasNextInt()) {
mainMenuChoice.add(scanInt.nextInt());      // exeption here
if(mainMenuChoice.get(mainI) &gt; mainMenu.length || mainMenuChoice.get(mainI) &lt;= 0) {
mainWhilePass = false;
mainMenuChoice.remove(mainI);
} else {
mainWhilePass = true;
}
} else {
System.out.print(&quot;not valid choice: &quot; + scanInt.next());
}
} catch (Exception e) {
System.err.println(&quot;Error: &quot; + e);
scanInt.next();
}

huangapple
  • 本文由 发表于 2020年5月5日 15:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61607574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定