1
Build a program that is able to average all entire or actual arguments received from the keyboard and print them.
Invalid arguments should be disregarded (Print a message for each invalid value. ), without triggering exception display (Display a message to the user asking him to inform again the value).
The user must enter S when they wish to terminate the entry dice;
I switched the S
for 5
, but it must be S
, how do I get to read a char
in place? I couldn’t get the logic.
import java.util.Scanner;
public class Media {
public static void main(String[] args) {
int i = 0;
float valor = 0;
float media = 0;
Scanner v = new Scanner(System.in);
valor = v.nextFloat();
while(valor != 5){
System.out.println("Insira um valor: ");
media += valor;
i++;
}
media = valor/i;
System.out.println("Média é: "+ media);
}
}
You can use Wrappers?
– user28595
The loop infinite occurs because the
valor
never changes inside the loop, so there is no way out. This answers the title question. The question of the question body is completely different from the title and to solve this you have to redo almost every code.– Maniero