gpt4 book ai didi

java - 扫描仪无法读取小数点

转载 作者:行者123 更新时间:2023-12-01 17:18:11 25 4
gpt4 key购买 nike

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);
}

}

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

最佳答案

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

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

因此您需要使用nextDouble()

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

关于java - 扫描仪无法读取小数点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com