所以,我是编程新手,我只是为了好玩而尝试制作一个基本的摩尔(化学)计算器。我没有找到这个问题。如果有人回答,请将链接发给我。
这是公式:n = N/Na
其中 n = 摩尔
和 Na = 6.022E23
代码的第一部分抛出错误。只是试图得到一个,将 Na 除以我给定的 N,即使使用 6.022 而不是 6.022E23 我得到 1000.0 作为答案。
Scanner in = new Scanner(System.in);
double Na = 6.022;
System.out.print("What do you want to know? Mol(0) or N(1)? ");
int first = in.nextInt();
if (first == 0){
System.out.print("Insert N: ");
double N = in.nextDouble();
double mol = N/Na;
System.out.print("There are " + mol + " mol in that sample.");
}
else if (first == 1){
System.out.print("Insert mol: ");
double mol = in.nextDouble();
double N = mol*Na;
System.out.print("There are " + N + " molecules, atoms or ions in that sample.");
}
输出 0:
What do you want to know? Mol(0) or N(1)? 0
Insert N: 6.022
There are 1000.0 mol in that sample.
输出 1:
What do you want to know? Mol(0) or N(1)? 1
Insert mol: 1
There are 6.022 molecules, atoms or ions in that sample.
提前致谢。
最佳答案
由于您正在编写一小段代码,因此我可以对新编码员提出的简单建议是硬编码,因此它是:
double mol = N/6.022;
这给了你你想要的。该错误是因为它将它视为 6022 而不是 6.022,因为它正在识别 .作为千位指标。希望这会有所帮助。
如果您不想对值进行硬编码,请添加:
in.useLocale(Locale.US);
就在您声明扫描仪的位置下方。这两种解决方案都应该可以解决您的问题。
关于java - 除法时得到 1000 而不是 1 (6.022/6.022=1000) Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952912/