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);
}
}大家好。这是我的代码,当我输入小数点时,它不能运行。如何修复它?
发布于 2013-12-15 22:12:10
用input.nextDouble();替换input.nextInt();
x = input.nextInt(); // It will simply ignore decimal values因此,您需要使用nextDouble()
x = input.nextDouble(); // This will read the entire decimal value发布于 2013-12-15 22:12:31
您可以使用以下命令读取整型
input.nextInt();试一试
input.nextDouble();发布于 2017-07-31 20:46:33
试试看。
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);
}
}https://stackoverflow.com/questions/20595436
复制相似问题