How to get correct output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get correct output?

I'm writing a code with java where i have to put numbers. In the end code should calculate how many times I have written number 1 and how many numbers there are altogether. And then get mean of how many times there were nr.1. I'm doing this with while - if method. With number 0 code should end. All of this I've been able to do. But if I wrote 0 already in the beginning, code should inform "no output". How to add it to the code?

10th Jan 2019, 10:59 AM
Ville Karimies
4 Answers
+ 2
Hmmm. How about posting the code of what you have done already so we can check how you went about solving your problem?
10th Jan 2019, 8:38 PM
Lambda_Driver
Lambda_Driver - avatar
0
int share = 0; int total = 0; double outcome = 0; while (true){ int number = Integer.valueOf(lukija.nextLine()); if (number == 0){ break; } else if (number == 1){ share = (share + 1); total = (total +1); } else if (number > 1){ total = (total +1); } outcome = 1.0 * share / total; } System.out.print on(outcome);
10th Jan 2019, 10:08 PM
Ville Karimies
0
Now the problem was that if I would insert number 0 in the beginning, code should have output "share of number ones couldn't be counted"
10th Jan 2019, 10:09 PM
Ville Karimies
0
Thank you very much! So simple..
15th Jan 2019, 8:43 PM
Ville Karimies