Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 117,572 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,965 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



finding the min and max of 3 input numbers

 
Reply to this topicStart new topic

finding the min and max of 3 input numbers

haloegirl
post 8 Sep, 2007 - 09:54 PM
Post #1


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 14


My Contributions


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

CODE

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: 9 Sep, 2007 - 02:12 AM
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 8 Sep, 2007 - 09:59 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,604



Thanked 116 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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! smile.gif
User is offlineProfile CardPM

Go to the top of the page

alpha02
post 8 Sep, 2007 - 10:00 PM
Post #3


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
My Contributions


To get the max:

CODE
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: 8 Sep, 2007 - 10:02 PM
User is offlineProfile CardPM

Go to the top of the page

haloegirl
post 9 Sep, 2007 - 10:24 AM
Post #4


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 14


My Contributions


QUOTE(alpha02 @ 8 Sep, 2007 - 10:00 PM) *

To get the max:

CODE
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
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;
        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: 9 Sep, 2007 - 12:18 PM
User is offlineProfile CardPM

Go to the top of the page

alpha02
post 9 Sep, 2007 - 10:30 AM
Post #5


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
My Contributions


QUOTE
CODE
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:

CODE

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.
User is offlineProfile CardPM

Go to the top of the page

haloegirl
post 9 Sep, 2007 - 10:42 AM
Post #6


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 14


My Contributions


QUOTE(alpha02 @ 9 Sep, 2007 - 10:30 AM) *

QUOTE
CODE
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:

CODE

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)

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();
            
            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: 9 Sep, 2007 - 12:19 PM
User is offlineProfile CardPM

Go to the top of the page

haloegirl
post 9 Sep, 2007 - 11:23 AM
Post #7


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 14


My Contributions


[quote name='haloegirl' date='9 Sep, 2007 - 10:42 AM' post='254052']
[quote name='alpha02' post='254046' date='9 Sep, 2007 - 10:30 AM']
[quote]
CODE
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];

}
}
[/quote]

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:

CODE

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.
[/quote]


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");

}

}
CODE

[/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! ! ");
                
    }
    
}

User is offlineProfile CardPM

Go to the top of the page

alpha02
post 9 Sep, 2007 - 11:56 AM
Post #8


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
My Contributions


QUOTE
CODE
//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:

CODE
//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!
User is offlineProfile CardPM

Go to the top of the page

haloegirl
post 9 Sep, 2007 - 12:03 PM
Post #9


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 14


My Contributions


QUOTE(alpha02 @ 9 Sep, 2007 - 11:56 AM) *

QUOTE
CODE
//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:

CODE
//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 biggrin.gif
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.gif
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 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: 9 Sep, 2007 - 12:20 PM
User is offlineProfile CardPM

Go to the top of the page

alpha02
post 9 Sep, 2007 - 12:19 PM
Post #10


D.I.C Addict

Group Icon
Joined: 20 May, 2006
Posts: 679



Dream Kudos: 825
My Contributions


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

Go to the top of the page