0
Ex) Write an algorithm that averages the numbers typed by the user if they are even. Finish reading if the user type zero.
I did so:
public static void main(String[] args){
Scanner x = new Scanner (System.in);
System.out.println("Digite um número: ");
int numero = x.nextInt();
while(numero < 0 | numero > 0){
System.out.println("Digite outro número");
/*
*imagino que eu tenha que usar outra variável, mas isso prejudicaria
*no inicio do meu while, que diz q a variável número não pode ser 0.
*/
numero = x.nextInt();
/*
*existe maneira de guardar vários valores em uma mesma variável?(imagino
*que seja inviável).
*/
Could someone show me how the algorithm gets solved using while?
the way it was before editing until it worked, the only problem is that the average was not coming out with the right result. now after editing it’s averaging the last two numbers typed. I did just as you explained: while(number != 0){ System.out.println("Type another number"); number = x.nextDouble(); sum += number; qnt++; } System.out.println("Media ="+(somatorio/qnt)); } } but when I do for example, 10, 8, 6, 0. instead of the average coming out 8 that would be right, it comes out 4,666. Anyway thank you very much.
– João Laurent
worked out, thank you!!!
– João Laurent
just rolling a little problem, it’s ignoring the first value provided, when calculating the average.
– João Laurent
Adjusted, now it’s working
– Luiz Lanza
perfect, thank you, now I’ll study your code kkk. hug
– João Laurent
Only problem is if the first number is odd or zero.
– Luiz Lanza
I realized another answer with a solution that meets all options
– Luiz Lanza
if it is zero he for the program, but this is right, only problem is when the first is odd q the average of wrong, but it does not catch anything, was worth the strength ai man!
– João Laurent
In need we are available =)
– Luiz Lanza