Java异常Exception in thread “main“ java.util.InputMismatchException

问题

今天在做表达式计算的题目时,写完代码后发现有数据没过,报出异常

Exception in thread "main" java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:947)
        at java.base/java.util.Scanner.next(Scanner.java:1602)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2267)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2221)
        at everydayjava.readInNumber(everydayjava.java:45)
        at everydayjava.main(everydayjava.java:10)

 发现是输入不匹配期望值,比如当我输入1=时,就会报错,识别为无效输入,中间输入空格后就可以有效识别了

Java异常Exception in thread “main“ java.util.InputMismatchException_第1张图片

Java异常Exception in thread “main“ java.util.InputMismatchException_第2张图片

原始代码如下

import java.util.Scanner;

public class everydayjava {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int one, two, three;
        char left, right;

        one = readInNumber(scanner);
        left = readInChar(scanner);

        if (left == '=') {
            System.out.println(one);
        } else {
            two = readInNumber(scanner);
            right = readInChar(scanner);

            if (right == '=') {
                int result = calculate(one, left, two);
                System.out.println(result);
            } else {
                while (right != '=') {
                    three = readInNumber(scanner);

                    if (check(left, right) == 0) {
                        one = calculate(one, left, two);
                        two = three;
                        left = right;
                    } else {
                        two = calculate(two, right, three);
                    }

                    right = readInChar(scanner);
                }

                int result = calculate(one, left, two);
                System.out.println(result);
            }
        }
    }

    public static int readInNumber(Scanner scanner) {
        int a = scanner.nextInt();
        return a;
    }

    public static char readInChar(Scanner scanner) {
        char b;
        do {
            b = scanner.next().charAt(0);
        } while (b == ' ');
        return b;
    }

    public static int check(char b1, char b2) {
        if (b1 == '*' || b1 == '/')
            return 0;
        else if (b2 == '*' || b2 == '/')
            return 1;
        else
            return 0;
    }

    public static int calculate(int x, char b, int y) {
        if (b == '*')
            return x * y;
        if (b == '/')
            return x / y;
        if (b == '+')
            return x + y;
        if (b == '-')
            return x - y;
        return 0;
    }
}


但是题目的后台数据报错应该没有我输入的这么简单,暂时想不到如何解决,留存博客。

后面加上了数据异常报错(作为一个题目这样肯定无法解决)代码如下

import java.util.Scanner;

public class everydayjava {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int one, two, three;
        char left, right;

        one = readInNumber(scanner);
        left = readInChar(scanner);

        if (left == '=') {
            System.out.println(one);
        } else {
            two = readInNumber(scanner);
            right = readInChar(scanner);

            if (right == '=') {
                int result = calculate(one, left, two);
                System.out.println(result);
            } else {
                while (right != '=') {
                    three = readInNumber(scanner);

                    if (check(left, right) == 0) {
                        one = calculate(one, left, two);
                        two = three;
                        left = right;
                    } else {
                        two = calculate(two, right, three);
                    }

                    right = readInChar(scanner);
                }

                int result = calculate(one, left, two);
                System.out.println(result);
            }
        }

        scanner.close(); // 在程序结束后关闭Scanner
    }

    public static int readInNumber(Scanner scanner) {
        if (scanner.hasNext()) {
            String input = scanner.next();
            try {
                return Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.println("无法解析为整数: " + input);
                return 0;
            }
        } else {
            return 0;
        }
    }

    public static char readInChar(Scanner scanner) {
        char b;
        do {
            b = scanner.next().charAt(0);
        } while (b == ' ');
        return b;
    }

    public static int check(char b1, char b2) {
        if (b1 == '*' || b1 == '/')
            return 0;
        else if (b2 == '*' || b2 == '/')
            return 1;
        else
            return 0;
    }

    public static int calculate(int x, char b, int y) {
        if (b == '*')
            return x * y;
        if (b == '/')
            return x / y;
        if (b == '+')
            return x + y;
        if (b == '-')
            return x - y;
        return 0;
    }
}

java初学,希望有大佬能指正

你可能感兴趣的:(java,开发语言)