【问题标题】:how do i put a loop in a try catch statement in java [duplicate]我如何在java中的try catch语句中放置一个循环[重复]
【发布时间】:2020-05-10 11:18:09
【问题描述】:

我的程序假设当用户输入一个整数时将 10 添加到一个随机数数组中,将显示随机数数组并在第一个数组下添加 10,如果用户没有输入一个 int然后 try catch 语句捕获显示错误消息的错误,所以我想要做的是在 try catch 语句中添加一个循环,使用户在他们不输入时输入一个 int 这是我迄今为止尝试过的并且没有不工作

public class tryandcatch {

    public static void main(String[] args) {

        int[] tab=new int[10];
        int i;
        Scanner inp=new Scanner(System.in);
        while(true) {
        try{
        System.out.println("Please enter an integer number");
        i=inp.nextInt(); 


        for(i=0;i<tab.length;i++){
            tab[i]=((int)(Math.random()*100));
            System.out.print(tab[i]+" ");
        }
        addTen(tab);
        System.out.print("\n");

        for(i=0;i<tab.length;i++)System.out.print(tab[i]+" ");
        break;
        }

        catch(InputMismatchException e){
            System.out.println("The number must be integer");

            i=inp.nextInt(); 


        }

        }

    }


    static void addTen(int[] x){
        int i;
        for(i=0;i<x.length;i++) x[i]+=10;
    }


    }

【问题讨论】:

    标签: java


    【解决方案1】:
    1. 使用Integer.parseInt(Scanner::nextLine()) 而不是Scanner::nextInt()。查看this 了解更多信息。
    2. 为简单起见,您可以使用boolean 变量来跟踪是否需要环回。

    按如下方式进行:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            int[] tab = new int[10];
            int n;
            Scanner inp = new Scanner(System.in);
            boolean valid;
            do {
                valid = true;
                try {
                    System.out.print("Please enter an integer number: ");
                    n = Integer.parseInt(inp.nextLine());
                    for (int i = 0; i < tab.length; i++) {
                        tab[i] = ((int) (Math.random() * 100));
                        System.out.print(tab[i] + " ");
                    }
                    addTen(tab);
                    System.out.println();
                    for (int i = 0; i < tab.length; i++) {
                        System.out.print(tab[i] + " ");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("The number must be integer");
                    valid = false;
                }
            } while (!valid);
        }
    
        static void addTen(int[] tab) {
            for (int i = 0; i < tab.length; i++) {
                tab[i] += 10;
            }
        }
    }
    

    示例运行:

    Please enter an integer number: a
    The number must be integer
    Please enter an integer number: 10.5
    The number must be integer
    Please enter an integer number: 5
    21 50 83 72 95 60 61 64 98 95 
    31 60 93 82 105 70 71 74 108 105 
    

    请注意,我使用了do...while,它保证循环块内的代码将至少执行一次。在这种特殊情况下,do...while 的使用也使代码更易于理解。但是,使用do...while 来解决此问题是可选的,如果您愿意,可以继续使用while 而不是do...while。

    【讨论】:

      【解决方案2】:

      使用break; 声明。

      此语句退出循环,因此将 try-catch 语句包含在 while(True) 循环中。如果未引发错误,则break; 语句退出循环。

      【讨论】:

        【解决方案3】:

        扫描器尝试解析输入,直到成功。在您的代码中,它总是尝试一次又一次地解析第一个非整数输入。为了解决这个问题,您可以读取用户输入而不尝试将其解析为int。您可以按如下方式更改您的捕获块:

              catch(InputMismatchException e){
                final String nonInteger = inp.next();
                System.out.println("The number must be integer. You typed: " + nonInteger);
              }
        

        【讨论】:

          猜你喜欢
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 2012-11-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多