Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
First I ask the user to input 10 numbers, then I have to find the max and min of those numbers. I can print out the maximum number of my input, but for I can't figure out how to print my minimum number.

What I have tried:

Java
import java.util.Scanner;
public class App {

    public static void main(String[] args) {
        int[] minAndMax = new int[10];
        Scanner scan = new Scanner(System.in);
        
  
        int smallest = minAndMax[0];
        int largest = minAndMax[0];
       
        
        for(int i = 0; i < minAndMax.length; i++ )
        {
           System.out.print("Enter in a number: ");
           minAndMax[i] = scan.nextInt();
        }
      
        for (int i = 0; i < minAndMax.length; i++)
        {
            if(minAndMax[i] > largest)
                largest = minAndMax[i];
            
            /*else if (minAndMax[i] < smallest)
            {
                smallest = minAndMax[i];
            */
      
        }   
        for(int i = 0; i > minAndMax.length; i++ ) 
        {
            if (minAndMax[i] < smallest)
                smallest = minAndMax[i];
        }
       
        System.out.println("Maximum Number: " + largest);
        System.out.println("Minimum Number: " + smallest);
    }
    
}
Posted
Updated 11-Oct-17 9:41am
Comments
Richard MacCutchan 11-Oct-17 15:35pm    
In addition to OriginalGriff's comment you should set smallest equal to largest, before you start the second loop.

int smallest = minAndMax[0];

What is minAndMax[0] initialized to?

Set smallest to the largest possible value and largest to the smallest possible value.
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;


Normally, the default value an Integer will be initialized to will be 0 if you don't specify a value.
Unless you type in a negative number, you already have 0 in the smallest.
Uncomment the code in your first loop but don't use an else. Get rid of the second loop which has an error in the for statement - it's not needed.

for (int i = 0; i < minAndMax.length; i++)
{
    if(minAndMax[i] > largest)
        largest = minAndMax[i];        
    if (minAndMax[i] < smallest)
        smallest = minAndMax[i];
}
 
Share this answer
 
Comments
Member 13458860 12-Oct-17 13:25pm    
@cvogt61457 Why does int smallest = Integer.MAX_VALUE; work? Can you explain how it is working in my code
cvogt61457 13-Oct-17 11:09am    
For 'smallest', you want to set it to the largest possible value. Then any number that the user types will be smaller than that.
You must initialize 'largest' to MIN_VALUE using the same reasoning.

Example: When 'smallest' is initialized to 0 and the user enters numbers with the smallest entered of 5, then 'smallest' is not the smallest value entered. When initialized to MAX_VALUE, any value the user types in will, by definition, be smaller than that MAX_VALUE.

You could also do this
for (...)
{
    smallest = Math.min(minAndMax[i], smallest);
    largest = Math.max(minAndMax[i], largest);
}
Because your loop never enters the body:
for(int i = 0; i > minAndMax.length; i++ ) 

Change the > for a < - it should work.
 
Share this answer
 
You should try to set smallest and largest initial values after the user have entered the values.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900