试图绕过错误:“main”java.util.InputMismatchException

时间:2018-01-20 17:41:12

标签: java

  

输入整数:99
  输入double:3.77
  输入字符:z
  输入字符串:你好   99 3.77 z你好

public class BasicInput {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int userInt = 99;
        double userDouble = 3.77;
        char userChar='z';
        String userString="Howdy";


        System.out.println("Enter integer: ");
        userInt = scnr.nextInt();

        System.out.print("Enter double : ");
        userDouble = scnr.nextDouble();

        System.out.print("Enter Character, ");
        userChar = scnr.next().charAt(0);

        System.out.print("Enter String, ");
        userString = scnr.next();

        System.out.println(userInt+" "+userDouble+" "+userChar+" "+userString);

        System.out.println(userString+" "+userChar+" "+userDouble+" "+userInt);

        int castInt = (int) userDouble;

        System.out.println(""+userDouble+" cast to an integer is "+castInt);

        return;

    }
}

尝试解决     输入整数:

  

线程“main”java.util.InputMismatchException中的异常         at java.util.Scanner.throwFor(Scanner.java:864)         在java.util.Scanner.next(Scanner.java:1485)         在java.util.Scanner.nextInt(Scanner.java:2117)         在java.util.Scanner.nextInt(Scanner.java:2076)         在BasicInput.main(BasicInput.java:13)

1 个答案:

答案 0 :(得分:0)

详细了解您收到的例外情况: https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html

  

由扫描程序抛出,表示检索到的令牌没有   匹配预期类型的​​模式,或者令牌不在   预期类型的​​范围。