如何在Java中从键盘读取double变量?我正在尝试解决这个问题,但是没有用。它说:


  java.util.Scanner.next(Scanner.java:1485)的java.util.Scanner.throwFor(Scanner.java:864)处的线程“ main”中的java.util.InputMismatchException(java.util.Scanner.nextDouble(位于Main.main(Main.java:7)的Scanner.java:2413)


我的密码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
        String s = scanner.next();
        double d = scanner.nextDouble();
        int myI = scanner.nextInt();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + myI);

    }
}

最佳答案

您还可以使用BufferedReader类将Double用作输入。
可以认为是

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String s=br.readLine();

Double d=Double.parseDouble(br.readLine());

int i=Integer.parseInt(br.readLine());

08-05 11:43
查看更多