11 Replies - 9555 Views - Last Post: 09 September 2007 - 12:32 PM Rate Topic: -----

#1 haloegirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
			System.out.print("Please enter your 1st test score: ");
			testScore1 = keyboard.nextInt();
			System.out.print("Please enter your 2nd test score: ");
			testScore2 = keyboard.nextInt();
			System.out.print("Please enter your 3rd test score: ");
			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)
				System.out.println("Your average score is " + averageScore + " and your grade is an F");
				
			else if(averageScore < 70)
				System.out.println("Your average score is " + averageScore + " and your grade is an D");
						
			else if(averageScore <80)
				System.out.println("Your average score is " + averageScore + " and your grade is an C");
								
			else if(averageScore<90)
				System.out.println("Your average score is " + averageScore + " and your grade is an B");
										
			else if(averageScore <=100)
				System.out.println("Your average score is " + averageScore +" and your grade is an A");
												
			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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#3 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 haloegirl  Icon User is offline

  • New D.I.C Head

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

Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 10:24 AM

View Postalpha02, 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
			System.out.print("Please enter your 1st test score: ");
			testScore1 = keyboard.nextInt();
			System.out.print("Please enter your 2nd test score: ");
			testScore2 = keyboard.nextInt();
			System.out.print("Please enter your 3rd test score: ");
			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)
				System.out.println("Your average score is " + averageScore + " and your grade is an F");
				
			else if(averageScore < 70)
				System.out.println("Your average score is " + averageScore + " and your grade is an D");
						
			else if(averageScore <80)
				System.out.println("Your average score is " + averageScore + " and your grade is an C");
								
			else if(averageScore<90)
				System.out.println("Your average score is " + averageScore + " and your grade is an B");
										
			else if(averageScore <=100)
				System.out.println("Your average score is " + averageScore +" and your grade is an A");
												
			else
				System.out.println("Invalid Score");
				
	}
	
}
EDIT: CODE TAGS

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

Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • 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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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.
Was This Post Helpful? 0
  • +
  • -

#6 haloegirl  Icon User is offline

  • New D.I.C Head

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

Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 10:42 AM

View Postalpha02, 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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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
			System.out.print("Please enter your 1st test score: ");
			testScore1 = keyboard.nextInt();
			System.out.print("Please enter your 2nd test score: ");
			testScore2 = keyboard.nextInt();
			System.out.print("Please enter your 3rd test score: ");
			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)
				System.out.println("Your average score is " + averageScore + " and your grade is an F");
				
			else if(averageScore < 70)
				System.out.println("Your average score is " + averageScore + " and your grade is an D");
						
			else if(averageScore <80)
				System.out.println("Your average score is " + averageScore + " and your grade is an C");
								
			else if(averageScore<90)
				System.out.println("Your average score is " + averageScore + " and your grade is an B");
										
			else if(averageScore <=100)
				System.out.println("Your average score is " + averageScore +" and your grade is an A");
												
			else
				System.out.println("Invalid Score");
				
	}
	
}
EDIT: CODE TAGS

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

Was This Post Helpful? 0
  • +
  • -

#7 haloegirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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']

View Postalpha02, 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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
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)
System.out.println("Your average score is " + averageScore + " and your grade is an F");

else if(averageScore < 70)
System.out.println("Your average score is " + averageScore + " and your grade is an D");

else if(averageScore <80)
System.out.println("Your average score is " + averageScore + " and your grade is an C");

else if(averageScore<90)
System.out.println("Your average score is " + averageScore + " and your grade is an B");

else if(averageScore <=100)
System.out.println("Your average score is " + averageScore +" and your grade is an A");

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
			System.out.print("Please enter your 1st test score: ");
			testScore1 = keyboard.nextInt();
			System.out.print("Please enter your 2nd test score: ");
			testScore2 = keyboard.nextInt();
			System.out.print("Please enter your 3rd test score: ");
			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)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an F");
				
			else if(averageScore < 70)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an D");
						
			else if(averageScore <80)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an C");
								
			else if(averageScore<90)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an B");
										
			else if(averageScore <=100)
				System.out.println("\nYour average score is " + averageScore +" and your grade is an A");
												
			else
				System.out.println("\nError invalid Score! ! ");
				
	}
	
}

Was This Post Helpful? 0
  • +
  • -

#8 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • 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!
Was This Post Helpful? 0
  • +
  • -

#9 haloegirl  Icon User is offline

  • New D.I.C Head

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

Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:03 PM

View Postalpha02, 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 :D
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 :crazy:
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
			System.out.print("Please enter your 1st test score: ");
			testScore1 = keyboard.nextInt();
			System.out.print("Please enter your 2nd test score: ");
			testScore2 = keyboard.nextInt();
			System.out.print("Please enter your 3rd test score: ");
			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)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an F");
				
			else if(averageScore < 70)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an D");
						
			else if(averageScore <80)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an C");
								
			else if(averageScore<90)
				System.out.println("\nYour average score is " + averageScore + " and your grade is an B");
										
			else if(averageScore <=100)
				System.out.println("\nYour average score is " + averageScore +" and your grade is an A");
												
			else
				System.out.println("\nError invalid Score! ! ");
				
	}
	
}
EDIT: CODE TAGS

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

Was This Post Helpful? 0
  • +
  • -

#10 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 haloegirl  Icon User is offline

  • New D.I.C Head

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

Re: finding the min and max of 3 input numbers

Posted 09 September 2007 - 12:25 PM

View Postalpha02, 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 :D
Was This Post Helpful? 0
  • +
  • -

#12 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 46
  • View blog
  • Posts: 803
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1