所以,我是编程的新手,我试图制作一个基本的鼹鼠(化学)计算器只是为了好玩.我没有找到这个问题.如果有人回答,请发给我链接.
这是公式:n = N / Nawhere n = mole和Na = 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 …Run Code Online (Sandbox Code Playgroud) 我仍然在编写代码,它在像我这样的项目中没有产生很大的不同,但如果我要做更大的事情,那将是一种痛苦.这里是:
case 0:
System.out.print("Insert the N: ");
double N = in.nextDouble();
double mol = N / Na;
System.out.print("There are " + mol + " mol in that sample");
break;
case 1:
System.out.print("Insert the m: ");
double m = in.nextDouble();
System.out.print("Insert the M: ");
double M = in.nextDouble();
double mol = m / M;
System.out.print("There are " + mol + " mol in that sample");
break;
case 2:
System.out.print("Insert the V: ");
double V = in.nextDouble();
double mol = V / …Run Code Online (Sandbox Code Playgroud) 我想打印一个没有十进制数字的浮点数.我知道我可以使用另一个像int这样的变量并使其相同,但我想避免这种情况更简单(如%02f但限制而不是至少两位数).
如果没有办法,那么我将使用变量int.
谢谢