简体   繁体   English

JAVA 线程“主”中的计算器异常 java.util.InputMismatchException

[英]JAVA CALCULATOR Exception in thread "main" java.util.InputMismatchException

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:943)
    at java.base/java.util.Scanner.next(Scanner.java:1598)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2569)
    at testCalculator.main(testCalculator.java:12)

I'm trying to make a working calculator, which reads even decimal numbers, but it doesn't work.我正在尝试制作一个工作计算器,它甚至可以读取十进制数,但它不起作用。 At the start it was integer valid only calculator, but within 30 minutes i tried to change it.一开始它是 integer 唯一有效的计算器,但在 30 分钟内我尝试更改它。 Sadly my efforts were for nothing, anybody can help me with it?可悲的是我的努力是徒劳的,有人可以帮助我吗? How to improve this calculator, so it can take decimal numbers without errors.如何改进这个计算器,让它可以无误地计算十进制数。 Thank you very much for every answer!非常感谢您的每一个回答!

import java.util.Scanner;

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

    while(true){
        System.out.println("First Number");
        double x = read.nextDouble();
        read.nextLine();
        System.out.println("Second Number");
        double y = read.nextDouble();
        read.nextLine();
        System.out.println("What you want to do? + / * - ");
        String z = read.nextLine();

        switch(z){
            case "+":
                System.out.println(x + y);
                break;
            case "-":
                System.out.println(x - y);
                break;

            case "*":
                System.out.println(x * y);
                break;

            case "/":
                System.out.println (x / y);
                break;
            default:
               System.out.println("bruh");
               break;

        }
    }

}

Given the locale info (pl_PL) the decimal separator is a comma, eg 21,15.给定语言环境信息 (pl_PL),小数点分隔符是逗号,例如 21,15。

To discover such things add the line要发现这样的事情,请添加行

System.out.println(java.util.Locale.getDefault());

Use the supported locales to look it up :使用支持的语言环境来查找它

https://www.oracle.com/java/technologies/javase/jdk8-jre8-suported-locales.html

And get further info on the locale:并获取有关语言环境的更多信息:

https://www.localepl.net.com/icu/pl-PL/index.html https://www.localepl.net.com/icu/pl-PL/index.html

and reference the "Decimal separator" value: , .并引用“小数点分隔符”值: ,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM