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