当我从用户那里获取输入时,为什么java向我显示异常?

当我从用户那里获取输入时,为什么java向我显示异常?,java,exception,sum,java.util.scanner,control-flow,Java,Exception,Sum,Java.util.scanner,Control Flow,当我给它一个字符串时,它给出异常,甚至不执行“sum和avg”值。如何更改代码以使其正常工作?如果我有一些错误的概念知识,请帮助我理解这个概念。多谢各位 对于扫描仪(System.in),您不需要扫描仪#hasNextInt。此外,您不需要检查,if(isAnInt)。相反,您应该放置一个try-catch块 您不应关闭扫描仪(System.in);否则,如果不重新启动JVM,就无法再次打开它 当您使用扫描仪处理文件时,上述两项都是必需的 演示: public static void input

当我给它一个字符串时,它给出异常,甚至不执行“sum和avg”值。如何更改代码以使其正常工作?如果我有一些错误的概念知识,请帮助我理解这个概念。多谢各位

  • 对于
    扫描仪(System.in)
    ,您不需要
    扫描仪#hasNextInt
    。此外,您不需要检查,
    if(isAnInt)
    。相反,您应该放置一个
    try-catch
  • 您不应关闭
    扫描仪(System.in)
    ;否则,如果不重新启动JVM,就无法再次打开它
  • 当您使用
    扫描仪
    处理
    文件
    时,上述两项都是必需的

    演示:

    public static void inputThenPrintSumAndAverage (){
            Scanner scanner = new Scanner(System.in);
            int count =0;
            int sum =0 ;
            long average = 0;
            boolean isAnInt = scanner.hasNextInt();
            while (true) {
                count++;
                int number = scanner.nextInt();
                if (isAnInt) {
                    sum+=number;
                    average = Math.round((sum/count));
                } else {
                    break;
                }
                scanner.nextLine();
            }
            System.out.println("SUM = "+sum+ " AVG = "+average);
            scanner.close();
        }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            long average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = Math.round((sum / count));
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            double average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = (double) sum / count;
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    
    运行示例:

    public static void inputThenPrintSumAndAverage (){
            Scanner scanner = new Scanner(System.in);
            int count =0;
            int sum =0 ;
            long average = 0;
            boolean isAnInt = scanner.hasNextInt();
            while (true) {
                count++;
                int number = scanner.nextInt();
                if (isAnInt) {
                    sum+=number;
                    average = Math.round((sum/count));
                } else {
                    break;
                }
                scanner.nextLine();
            }
            System.out.println("SUM = "+sum+ " AVG = "+average);
            scanner.close();
        }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            long average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = Math.round((sum / count));
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            double average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = (double) sum / count;
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    
    注意:如果将其声明为
    double
    并将浮点计算存储到其中而不进行舍入,则可以获得更高的平均精度,例如

    2
    3
    5
    8
    abc
    SUM = 18 AVG = 4
    
    此更改后的示例运行:

    public static void inputThenPrintSumAndAverage (){
            Scanner scanner = new Scanner(System.in);
            int count =0;
            int sum =0 ;
            long average = 0;
            boolean isAnInt = scanner.hasNextInt();
            while (true) {
                count++;
                int number = scanner.nextInt();
                if (isAnInt) {
                    sum+=number;
                    average = Math.round((sum/count));
                } else {
                    break;
                }
                scanner.nextLine();
            }
            System.out.println("SUM = "+sum+ " AVG = "+average);
            scanner.close();
        }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            long average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = Math.round((sum / count));
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            inputThenPrintSumAndAverage();
        }
    
        public static void inputThenPrintSumAndAverage() {
            Scanner scanner = new Scanner(System.in);
            int count = 0;
            int sum = 0;
            double average = 0;
            while (true) {
                try {
                    int number = scanner.nextInt();
                    sum += number;
                    count++;
                } catch (InputMismatchException e) {
                    break;
                }
            }
            average = (double) sum / count;
            System.out.println("SUM = " + sum + " AVG = " + average);
        }
    }
    

    理解它也很有用。

    您需要在while循环中测试
    。hasNextInt
    ,而不仅仅是在它之前。你需要在把每一个都放进去之前进行测试。您还应该在获取int后立即调用
    .nextLine()
    。但它仍然不起作用:(。再次显示异常。