So, I'm new to programming, and I was trying to make a basic mole (chemistry) calculator just for fun. I didn't find this question. If it was answered please send me the link.
This is the formula: n = N / Na
where n = mole
and Na = 6.022E23
The first part of the code throws an error. Just trying to get one, dividing Na by my given N, and even with 6.022 instead of 6.022E23 I'm getting 1000.0 as an answer.
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.");
}
Output 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.
Output 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.
Thanks in advance.