简体   繁体   English

按升序排序三个浮点数

[英]Sort in ascending order three floating point numbers

import java.util.Scanner;

public class Crescente {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double primo = in.nextDouble();
        double secondo = in.nextDouble();
        double terzo = in.nextDouble();
        if(primo > secondo && primo > terzo) {
            if(secondo > terzo) {
                System.out.println(primo+" "+secondo+" "+terzo);
            }else {
                System.out.println(primo+" "+terzo+" "+secondo);
            }
        }else if(secondo > primo && secondo > terzo) {
            if(primo > terzo) {
                System.out.println(secondo+" "+primo+" "+terzo);
            }else {
                System.out.println(secondo+" "+terzo+" "+primo);
            }
        }else if(terzo > primo && terzo > secondo) {
            if(primo > secondo) {
                System.out.println(terzo+" "+primo+" "+secondo);
            }else {
                System.out.println(terzo+" "+secondo+" "+primo);
            }
        }
        in.close();
    }
}

My program works if you enter integer numbers, but if you enter floating point numbers it gives me this error:如果您输入整数,我的程序可以工作,但是如果您输入浮点数,它会给我这个错误:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at poo.Crescente.main(Crescente.java:8)

I don't know why it gives me this error since I used nextDouble for all of the variables which are all double .我不知道为什么它会给我这个错误,因为我将nextDouble用于所有都是double的变量。 Please help.请帮忙。

Looks like your default locale is set to LOCALE.ITALY as you are from Italy.看起来您的默认语言环境设置为LOCALE.ITALY因为您来自意大利。 Change it to the following改成下面这样

Scanner in = new Scanner(System.in);
in.useLocale(Locale.ENGLISH);
// rest of the code

I was able to run the program with comma separated numbers (4,2) and decimal separated numbers (4.2) by toggling with the Locale.ITALY and Locale.ENGLISH respectively.通过分别切换Locale.ITALYLocale.ENGLISH我能够使用逗号分隔的数字 (4,2) 和十进制分隔的数字 (4.2) 运行程序。

From the doc's :从文档:

 useLocale(Locale locale)

Sets this scanner's locale to the specified locale.将此扫描器的语言环境设置为指定的语言环境。

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

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