+ 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?
4 ответов
+ 2
Hmmm. How about posting the code of what you have done already so we can check how you went about solving your problem?
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);
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"
0
Thank you very much! So simple..