No incluye un número como una variable de entrada del usuario y tener que incrementar otra variable?

kaiju:

Mis programas pide al usuario que introducir enteros (en un bucle) hasta que la entrada -99; que luego mostrar los números más altos y más bajos de los números enteros de entrada. Tengo una variable llamada recuento, que se incrementa cada vez que el usuario puts en un nuevo entero, para realizar un seguimiento del número de enteros introducidos por el usuario. ¿Cómo he -99 no incluido como uno de los números enteros y no incrementar el recuento?

Código:

//variables
        int num = 0, count = 0, high, low;
        Scanner userInput = new Scanner(System.in);


        low = num;
        high = num;

        //loop

        while(num != -99){
                    System.out.print("Enter an integer, or -99 to quit: --> ");
                    num = userInput.nextInt();
                    count++;



                    if (num == -99 && count == 0)
                    { 
                        count--;
                        System.out.println("You did not enter a number");

                    } //outer if end
                    else {



                    //higher or lower
                    if(count > 0 && num > high)
                    {
                       high = num; 
                    } //inner else end
                    else if(count > 0 && num < low)
                    {
                        low = num;
                    } //inner else if end
                    else
                    {

                    } //inner else end
                    } //outer else end
    }     


        System.out.println("Largest integer entered: " + high);
        System.out.println("Smallest integer entered: " + low);
Azhar:

Usted se acerca es bueno, pero se ha perdido algunos puntos,

  • su condición de encontrar max o min también es incorrecto porque usted tiene que escribir por separado.
  • El usuario introduce un valor o no, usted tiene que decidir esta fuera del bucle.
  • Usted tiene que inicializar alta y baja con la primera entrada. Estoy tratando de hacer alguna corrección en su programa, simplemente cambiando la parte requerida. Espero que te ayude.

		//variables
    int num = 0, count = 0, high =0 , low = 0;
    Scanner userInput = new Scanner(System.in);
    //loop

    while(true){
		//Using infinite loop, we will break this as per condition.
		System.out.print("Enter an integer, or -99 to quit: --> ");
		num = userInput.nextInt();
		if(num == -99)
		{
			break;
		}
		count++;

		if(count == 1)
		{//initialize high and low by first number then update
			high = num;
			low = num;
		}
		//to check highest
		if(num > high)
		{
		   high = num; 
		} 
		
		//to check smallest
		if(num < low)
		{
			low = num;
		}
		
                
}     
if (count == 0)
{//Here we check that if user enter any number or directly entered -99 
	System.out.println("You did not enter a number");
}
else
{
	System.out.println("Largest integer entered: " + high);
    System.out.println("Smallest integer entered: " + low);
}

        

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=371170&siteId=1
Recomendado
Clasificación