Find Max / Min Values ​​in a for Loop

I was looking for answers to this particular question, but could not find anything. I need to find the maximum and minimum of input numbers, but the required values ​​are inside the for loop, and I cannot figure out how to use them outside of it.

System.out.print("How many numbers do you want to input?"); int totalNumbers = console.nextInt(); int minMax = 0; for(int i = 1; i <= totalNumbers; i++){ System.out.print("Number " + i + ": "); int inputNumbers = console.nextInt(); } System.out.println(); int smallest = Math.min(minMax); System.out.println("Smallest = " + smallest); int largest = Math.max(minMax); System.out.println("Largest = " + largest); 

I don't need the modified code, just something that will help me on the right track. Thanks!

+6
source share
5 answers

Can you spot a problem with the next loop?

 for(int i = 1; i <= totalNumbers; i++){ System.out.print("Number " + i + ": "); int inputNumbers = console.nextInt(); } 

You run the totalNumbers once and every time you create a new int called inputNumbers and save the resulting value from the console. Also where do you change the minMax value? In addition, Math.min(or max) does not accept single parameters and does not even compile.

You now have several options:

  • Either store all the numbers in an array, and then cross this for the min and max elements using some utility method.
  • Set some minimum and maximum value and run the loop to get all the elements, as well as keep track of the min and max cycles.

I am not writing any solution, because I want you to try it yourself.

+4
source

The methods Math.min () and Math.max (), according to oracle documentation, can only compare two values. Importing values ​​into an array and then performing operations on the array should allow you to easily find the minimum and maximum values, as well as any other data operation.

 int[] numbers = new int[totalNumbers]; for (int i = 0; i < totalNumbers; i++) { numbers[i] = console.nextInt(); } //Other Operations 
+4
source

If I misunderstand what you want, can you just save the variable outside the for loop and check them out?

 int minMax = 0; int smallest = 0; int largest = 0; for(int i = 1; i <= totalNumbers; i++){ System.out.print("Number " + i + ": "); int inputNumbers = console.nextInt(); if(inputNumbers > largest){ largest = inputNumbers; } else if (inputNumbers < smallest){ smallest = inputNumbers; } } System.out.println(); System.out.println("Smallest = " + smallest); System.out.println("Largest = " + largest); 

This is the most direct and logical way to check the input values ​​and decide whether they are the smallest or the largest currently known ( Edit: If you do not need to use Math.minMax )

+3
source

pseudo code:

 smallest := +∞ largest := -∞ for each number read, n: if n > largest: largest := n if n < smallest: smallest := n print the results 

Hint:

Java int cannot represent ± ∞. Use Integer.MIN_VALUE and Integer.MAX_VALUE .

+2
source

You can do this, just put an if else condition inside the loop ... for example

  • take 2 variables in the outer loop, one for the maximum value and the other to store the min value.
  • the inner loop assigns the input numbers min and max for the first time.
  • after that comare the next number with this and reassign the values.
  • At the end of the loop, you will find both min and max.
+1
source

All Articles