Build a program that is capable of averaging all whole or actual arguments received from the keyboard and print them.
Invalid arguments should be disregarded (Print a message for each invalid value.), without triggering exceptions (Show message to the user asking him to value).
The user must enter S when you want to close the data;
I changed S
to 5
, but should be S
, how do I get a char
to read in place? I could not 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);
}
}