Problem with For Loop and Socks

2

I have had a problem when doing an exercise that asks the user for the number of temperature values taken (it must be greater than 0) and then ask for temperatures and return the number of times it has a value less than zero degrees and the average temperature. Currently I have done so: (Declare all variables as Int).

System.out.println("Introduzca el numero de temperaturas(POSITIVOS)");
temperaturas = entrada.nextInt();
do
{
for (ntemperaturas = 1; ntemperaturas <= temperaturas ; ntemperaturas++)
{
System.out.println("Temperatura numero " + ntemperaturas + ":" );
temperatura = entrada.nextInt();
}
}
while (temperaturas>0);

I would like to know what I am wrong about and how to correct it in order to complete the above statement. Thank you very much!

    
asked by Keide 14.10.2018 в 14:27
source

1 answer

0

The condition of While will always be true, for this reason it will never leave the loop, you are creating an infinite loop. One of the possible solutions is to eliminate the for, because it is unnecessary and each time you are going through each temperature you lower the temperatures in one and also creating an iterator, something like this:

System.out.println("Introduzca el numero de temperaturas(POSITIVOS)");
temperaturas = entrada.nextInt();
int i = 0;
do{

    System.out.println("Temperatura numero " + i + ":" );
    int temperatura = entrada.nextInt();
    System.out.println("La temperatura: " + i + " es: " + temperatura);
    i++;

}while (i < temperaturas);
    
answered by 14.10.2018 в 18:39