Yang
Yang

Reputation: 43

using a while loop for user to input multiple int and find the max and min

so my homework question is prompt user a series of integers and find the max and min of those integer. Use a loop and -99 to break loop.Below is my code but my question is that is there a shorter way for this? feel free to comment and i appreciate your time reading this.

  Scanner input=new Scanner(System.in);
  int num1,num2,max,min;
  System.out.print("enter a number: ");
  num1=input.nextInt();
  System.out.print("enter another number: ");
  num2=input.nextInt();
  max=Math.max(num1,num2);
  min=Math.min(num1,num2);


  while (num2!=-99){
      System.out.print("enter a number or -99 to stop: ");
      num2=input.nextInt();
      if(num2!=-99){
      max=Math.max(max,num2);
      min=Math.min(min,num2);
      }

  }
  System.out.println("largest is: "+max);
  System.out.println("Smallest is: "+min);

Upvotes: 4

Views: 2571

Answers (3)

SassyRegards201
SassyRegards201

Reputation: 66

Ok So after working on this. I finally did it. It does have a small bug, but I really don't wanna fix it so if anyway wants to edit this then be my guest.

 Scanner input = new Scanner(System.in);
    int studentNum = 0;
    ArrayList<Integer> calc = new ArrayList<Integer>();

    while (studentNum <= 100) {

        System.out.print("Enter a number: ");
        calc.add(input.nextInt());

        studentNum += 1;

        if (input.nextInt() == -99) {
            break;
        }

    }

    int min = Collections.min(calc);
    int max = Collections.max(calc);

    for (int i = 0; i < calc.size(); i++) {
        int number = calc.get(i);
        if (number < min)
            min = number;
        if (number > max)
            max = number;
    }

    System.out.println("Max is " + max);
    System.out.println("Min is " + min);

This does exactly what you want. However, there was a problem checking for the exit signal.

 if (input.nextInt() == -99) {
            break;
        }

this checks if the userInput is equal to -99 then stops the program and calculates and prints out the min and max. However the tiny bug is that it will first ask for your number to add to the array list and then it will ask again for the userInput to check if its equal to -99. But overall it does exactly what you want.

Hope this helped.

EDIT I will work on it later and find a way to fix that bug if no one else knows.

Upvotes: 0

Hugo G
Hugo G

Reputation: 16496

Your code has one minor bug and can be slightly optimised. Things you might want to consider:

  • What happens (and what should happen) when the user types -99 as the second number?
  • Do you necessarily need to have at least 2 numbers? Wouldn't one be enough for the program to exit gracefully? The first number would then be both min and max.
  • Can you re-order your code lines so that the duplicated (num2!=-99) is not necessary anymore?

Pro questions:

  • What happens if the user types in some letters? How can you handle that case?
  • What happens if the user types in a super high number (bigger than the maximal integer)?
  • What happens if the user presses enter without typing any number?

Nitpicking:

Upvotes: 0

Mitch
Mitch

Reputation: 3418

You check for the condition of num2 != -99 twice, remember the first rule of programming, do not repeat yourself

You could save some lines by checking for min and max before asking for the next input. This way you do not need to check if num2 != -99 inside the while loop

Upvotes: 2

Related Questions