# finding the min and max of 3 input numbers

Page 1 of 1

## 11 Replies - 17657 Views - Last Post: 09 September 2007 - 12:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=32923&amp;s=d0058cbdc203dc23db5c9252ca4dc3d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

# finding the min and max of 3 input numbers

Posted 08 September 2007 - 09:54 PM

I am trying to complete this program that will allow the user to enter 3 test scores display the average, the letter grade, the minimum score and the maximum score. I am able to do everything except displaying the min and max score that the user enters. Could someone please help, I would really appriciate it : D

```import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1;
int testScore2;
int testScore3;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();
System.out.print("Your highest test score is: "  );
System.out.println();
System.out.print("Your lowest test score is: "  );
System.out.println();
//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("Invalid Score");

}

}
```
EDIT: CODE TAGS

This post has been edited by PennyBoki: 09 September 2007 - 02:12 AM

Is This A Good Question/Topic? 0

## Replies To: finding the min and max of 3 input numbers

### #2 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

## Re: finding the min and max of 3 input numbers

Posted 08 September 2007 - 09:59 PM

The basic idea is that you take the first number and put it in a variable. Take the second number and compare it to the first. Is it bigger? Yes, put it in the variable to replace the lower one. Check the third number, is it bigger? Yes? Replace the variable again. This will give you the max of the three numbers at the end, whatever is in the variable is the biggest.

For the minimum you do something similar but instead of replacing the variable with the bigger number, you will replace it if it is the smallest. Record the first number, compare it against the second, is it smaller? No? Move to the third number. Is it smaller? Yes? ok, replace the variable with the smaller number. At the end you have the smallest number.

You should be able to whip up a small function to do this in just a few lines to compare the numbers against the recorded first number.

Good luck to you!

### #3 Liontrue1

Reputation: 48
• Posts: 811
• Joined: 20-May 06

## Re: finding the min and max of 3 input numbers

Posted 08 September 2007 - 10:00 PM

To get the max:

```int[] nums = new int[]{1, 4, 2, 5, 3, ...};
int max = 0;
for (int i=0; i<nums.length; i++){
if (nums[i]>max){
max = nums[i];
}
}
System.out.println("Max: "+max);
```

This should get you started. Hope this helps. Just reverse the process to get the minimum.

This post has been edited by alpha02: 08 September 2007 - 10:02 PM

### #4 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 10:24 AM

alpha02, on 8 Sep, 2007 - 10:00 PM, said:

To get the max:

```int[] nums = new int[]{1, 4, 2, 5, 3, ...};
int max = 0;
for (int i=0; i<nums.length; i++){
if (nums[i]>max){
max = nums[i];
}
}
System.out.println("Max: "+max);
```

This should get you started. Hope this helps. Just reverse the process to get the minimum.

I tried to use this in my program but it displays 0 for highest score...?? I am not sure what i have done wrong. Please help
```import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
int min = 0;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
System.out.print("Your highest test score is: " + max);
System.out.println();

//To display lowest score
System.out.print("Your lowest test score is: " + min);
System.out.println();
//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("Invalid Score");

}

}
```
EDIT: CODE TAGS

This post has been edited by PennyBoki: 09 September 2007 - 12:18 PM

### #5 Liontrue1

Reputation: 48
• Posts: 811
• Joined: 20-May 06

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 10:30 AM

Quote

``` int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
int min = 0;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

You created the nums array before setting the testScore variables, causing the array to contain (0, 0, 0). Instead, set the testScore variables BEFORE creating the array, as follow:

```int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;

int min = 0;

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

Hope this helps.

### #6 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 10:42 AM

alpha02, on 9 Sep, 2007 - 10:30 AM, said:

Quote

``` int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
int min = 0;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

You created the nums array before setting the testScore variables, causing the array to contain (0, 0, 0). Instead, set the testScore variables BEFORE creating the array, as follow:

```int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;

int min = 0;

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

Hope this helps.

ok now that the max works, i tried to reverse the code to fine the min and i am getting an error that says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at TestScores.main(TestScores.java:44)

```import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
System.out.print("Your highest test score is: " + max);
System.out.println();

//To display lowest score
int min =0;
for (int i = 0; i < nums.length; i --)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();

//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("Invalid Score");

}

}
```
EDIT: CODE TAGS

This post has been edited by PennyBoki: 09 September 2007 - 12:19 PM

### #7 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 11:23 AM

[quote name='haloegirl' date='9 Sep, 2007 - 10:42 AM' post='254052']

alpha02, on 9 Sep, 2007 - 10:30 AM, said:

Quote

``` int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
int min = 0;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

You created the nums array before setting the testScore variables, causing the array to contain (0, 0, 0). Instead, set the testScore variables BEFORE creating the array, as follow:

```int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;

int min = 0;

//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
```

Hope this helps.

ok now that the max works, i tried to reverse the code to fine the min and i am getting an error that says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at TestScores.main(TestScores.java:44)

import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
//To display highest score
for (int i = 0; i < nums.length; i ++)
{

if (nums[i] > max)
{
max = nums[i];

}
}
System.out.print("Your highest test score is: " + max);
System.out.println();

//To display lowest score
int min =0;
for (int i = 0; i < nums.length; i --)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();

//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("Invalid Score");

}

}
```[/quote]

Thanks for the help with the max... but now I have changed some more and now the min will only display 0 as the lowest score... do i need to have the array before the if statement as i did in the max??? is that why it only displays 0 ??? help please
[code] import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//Array to hold user input
int [] nums = new int [] {testScore1, testScore2, testScore3};
int min = 0;
int max = 0;
//To display highest score
for (int i = 0; i < nums.length; i ++)
{
if (nums[i] > max)
{
max = nums[i];
}

}
System.out.print("\nYour highest test score is: " + max);

//To display lowest score
for (int i = 0; i >nums.length; i ++)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("\nYour lowest test score is: " + min);

//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

//To display average score and letter grade
if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("\nError invalid Score! ! ");

}

}
```

### #8 Liontrue1

Reputation: 48
• Posts: 811
• Joined: 20-May 06

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 11:56 AM

Quote

``` //To display lowest score
int min =0;
for (int i = 0; i < nums.length; i --)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();
```

Make the "i" variable increment in the loop, because it looks through all elements of the array, and -1 isn't an element! The code should be:

```//To display lowest score
int min =0;
for (int i = 0; i < nums.length; i ++)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();
```

Hope this helps, any questions then just reply!

### #9 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:03 PM

alpha02, on 9 Sep, 2007 - 11:56 AM, said:

Quote

``` //To display lowest score
int min =0;
for (int i = 0; i < nums.length; i --)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();
```

Make the "i" variable increment in the loop, because it looks through all elements of the array, and -1 isn't an element! The code should be:

```//To display lowest score
int min =0;
for (int i = 0; i < nums.length; i ++)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("Your lowest test score is: " + min);
System.out.println();
```

Hope this helps, any questions then just reply!

Thanks for the help and I hope you can help me again
I have changed the code to what you wrote and it shows the lowest score
as 0 still... I dont understand what i am doing wrong.
Locially just reversing the code for max I figured it would work, but
I get a 0 as the lowest score still
Could you please take a look and see .. i am sure it is some little
thing that iam missing and i just dont see it
```import java.util.Scanner;

public class TestScores

{
public static void main(String[] args)
{
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;

//User Prompts
testScore1 = keyboard.nextInt();
testScore2 = keyboard.nextInt();
testScore3 = keyboard.nextInt();

//Array to hold user input
int [] nums = new int [] {testScore1, testScore2, testScore3};
int max = 0;
//To display highest score
for (int i = 0; i < nums.length; i ++)
{
if (nums[i] > max)
{
max = nums[i];
}

}
System.out.print("\nYour highest test score is: " + max);

//To display lowest score
int min = 0;
for (int i = 0; i < nums.length; i++)
{
if(nums[i] < min)
{
min = nums[i];
}
}
System.out.print("\nYour lowest test score is: " + min);

//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;

//To display average score and letter grade
if (averageScore < 60)

else if(averageScore < 70)

else if(averageScore <80)

else if(averageScore<90)

else if(averageScore <=100)

else
System.out.println("\nError invalid Score! ! ");

}

}
```
EDIT: CODE TAGS

This post has been edited by PennyBoki: 09 September 2007 - 12:20 PM

### #10 Liontrue1

Reputation: 48
• Posts: 811
• Joined: 20-May 06

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:19 PM

Do not set the min variable to 0 at the start (int min = 0) but set it to testScore1. Test it.

### #11 haloegirl

Reputation: 0
• Posts: 14
• Joined: 01-August 07

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:25 PM

alpha02, on 9 Sep, 2007 - 12:19 PM, said:

Do not set the min variable to 0 at the start (int min = 0) but set it to testScore1. Test it.

OMG it works.. thanks I really appriciate your help

### #12 Liontrue1

Reputation: 48
• Posts: 811
• Joined: 20-May 06

## Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:32 PM

No need to say My God, just call me alpha! And you're welcome!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }