Java Scanner - 双重错误

时间:2015-07-24 14:51:17

标签: java double

我有扫描仪的问题。如果我尝试使用double,控制台会给我错误消息:

  

线程中的异常" main" java.util.InputMismatchException                    at java.util.Scanner.throwFor(Scanner.java:864)                    在java.util.Scanner.next(Scanner.java:1485)                    在java.util.Scanner.nextDouble(Scanner.java:2413)                    在test.test.main(test.java:9)

package test;

import java.util.Scanner;

public class test {
public static void main(String args[]){
    System.out.print("vlozte hmotnost: ");
    Scanner vstup = new Scanner (System.in);
    double hmotnost = vstup.nextDouble();

//------------------------------------------        

    System.out.print("vlozte drahu: ");
    Scanner vstup2 = new Scanner(System.in);
    double draha = vstup2.nextDouble();

//------------------------------------------

    double sila = hmotnost * 10;

//------------------------------------------

    double praca = sila * draha;
    System.out.print("praca je: ");
    System.out.print(praca);
    System.out.println(" Joulov");

2 个答案:

答案 0 :(得分:2)

输入 1,5 而不是 1.5 。另外,如上所述,您可以使用一台扫描仪:

import java.util.Scanner;

public class test {
    public static void main(String args[]) {
        System.out.print("vlozte hmotnost: ");
        Scanner vstup = new Scanner(System.in);
        double hmotnost = vstup.nextDouble();

//------------------------------------------

        System.out.print("vlozte drahu: ");
        double draha = vstup.nextDouble();

//------------------------------------------

        double sila = hmotnost * 10;

//------------------------------------------

        double praca = sila * draha;
        System.out.print("praca je: ");
        System.out.print(praca);
        System.out.println(" Joulov");
    }
}

答案 1 :(得分:0)

首先,您不需要两台扫描仪。第一台扫描仪上的nextDouble()可以帮助您读取下一个值。

此外,我不明白为什么你会收到错误。我试过你的代码,它工作得很好。

输出:

vlozte hmotnost: 1.5
vlozte drahu: 4.5
praca je: 67.5 Joulov