2014-02-22 18 views
6

所以,我是编程新手,我试图制造一个基本的摩尔(化学)计算器只是为了好玩。我没有找到这个问题。如果有人回答,请给我链接。分割时得到1000而不是1(6.022/6.022 = 1000)Java

这是下式:其中n = N/Nan = moleNa = 6.022E23

代码的第一部分引发错误。只是试图得到一个,用我的N分割Na,甚至用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. 

预先感谢。

+6

看起来像'Locale'问题。你正在使用什么'Locale'? – Keppil

+1

@Keppil这就是我所期望的。在美国的系统中,点被解释为数千的分隔符,而不是小数点。 –

+0

@Keppil我不知道。新来的,从来没有听说过这个词。我想我明白你的意思了。不,这不是问题。我可以用点和逗号工作,都是一样的。但1000不是任何地方。 – EnderEgg

回答

5

既然你写一个小码,简单化的建议,新的编码器,我可以做是硬编码它,所以它是:

double mol = N/6.022; 

这给你想要的东西。错误是因为它将它看作6022而不是6.022,因为它正在认识到这一点。作为千位指标。希望这可以帮助。

如果你不想硬编码的值,加:

in.useLocale(Locale.US); 

右下方,你宣布你的扫描仪。这两种解决方案都应该解决您的问题。

+0

使用Bumptious Q Bangwhistle链接,并添加in.useLocale(Locale.US);解决了这个问题。尽管如此,我想保留Na的未来参考。谢谢你的时间。确实。该编辑正在回答所有内容! – EnderEgg

相关问题