A few days ago a community user asked this question, and once I answered it, he decided to delete it.
Since it had taken me some time to craft the answer to get the code to work, I'm taking the liberty of reposting the question and my answer.
The question was the following:
Good day. Could someone please help me with this error, I have to get a percentage from a range of numbers. I get the count correctly, but the arithmetic doesn't work for me to get the percentage.
public class Ej08 {
public static void main(String[] args) throws Exception {
int n;
int suma=0;
int bajo=0;
int inv=0;
float num;
double mbajo= 5.0;
float mybajo= 14;
double minv= 80.0;
float myinv= 100;
int i;
java.util.Scanner lea = new java.util.Scanner(System.in);
System.out.print("Indique la cantidad de numeros : ");
n = lea.nextInt();
if(n<=0)
System.out.println("El valor no es valido");
else {
for(i=1;i<=n;i++) {
System.out.print("Dato "+i+" : ");
num=lea.nextFloat();
suma += num;
if(num> mbajo && num<=mybajo){
bajo= bajo + 1;
System.out.println(" sumo "+ num);
}
if(num> minv && num <= myinv){
inv= inv + 1;
System.out.println(" sumo "+ num);
}
}
System.out.println("bajo trae: "+bajo);
System.out.println("inviable tre: "+inv);
System.out.println("n trae: "+n);
double prom= (suma/(n*1.0));
float pb=(bajo /n)*100;
float pi=(inv / n)*100;
/* if (prom>=01){
5.1 7 25 70 50 90
}*/
System.out.println("El promedio de los números es : "+prom);
System.out.println("bajo: "+pb);
System.out.println("inviable: "+pi);
}
lea.close();
In the code you can see that the low variable brings 2 and the inv variable brings 1 and the n variable brings 6 which is the number of times the program reads.
I need to know how much is under n, that is, how much is 2 of 6 is simple, it is divided and multiplied by 100, but it does not perform the operation because in the end pb and pi have 0.0 as a result
Let's see if I found out what you need and I'll give you a good answer
The low numbers are those between 6 and 14
The unfeasible numbers, those between 81 and 100
Every time you enter a number on the keyboard that falls between the previous parameters, you increase their respective counters.