无法读取扫描仪中的小数点

时间:2013-12-15 14:10:33

标签: java eclipse math double decimal

import java.util.Scanner;
public class Assignment3 {

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

        double pi = 3.142;
        double T, v, a, x, r; 


        System.out.println("3.Centrifuge. The rotor of an ultracentrifuge rotates at x rev/s.");
        System.out.println("A particle at the top of the test tube is  r meter from the rotation axis.");
        System.out.println("Calculate it’s centripetal acceleration.");


        System.out.printf("Enter the number of rotation in rev/s : ");
        x = input.nextInt();

        System.out.printf("Enter the distance in meter : ");
        r = input.nextInt();

        T = 1/x;
        v = (2*pi*r)/T;
        a = (v*v)/r;

        System.out.println("\n\nAnswer");
        System.out.printf("The centripetal acceleration is : %.2f m/s^2\n", a);
    }

}

大家好。这是我的编码,当我输入小数点时它无法运行。如何解决?

3 个答案:

答案 0 :(得分:3)

input.nextInt();替换为input.nextDouble();

x = input.nextInt(); // It will simply ignore decimal values

因此您需要使用nextDouble()

x = input.nextDouble(); // This will read the entire decimal value

答案 1 :(得分:1)

你用

读了一个int
input.nextInt();

尝试

input.nextDouble();

答案 2 :(得分:0)

试试吧。

import java.util.Scanner;

public class JavaScannerDouble {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String data = input.next();
    double decimalValue = Double.parseDouble(data);
    System.out.println("Decimal value : " + decimalValue);
    // test of decimal value
    double response = decimalValue / 0.1;

    System.out.println("response : " + response);
   }
}