-
Maximum Minimum Values
I'm working on some code and the book I have is being of no real help as well as in the internet since most people use finding max/min in arrays but I need to use it from data entered by the user.
Code:
import javax.swing.JOptionPane;
public class exam
{
public static void main(String [] args)
{
String inputString;
int testnum=1;
int testScore;
double average;
int max;
int min;
do
{
inputString=JOptionPane.showInputDialog("Please enter your test score.");
testScore=Integer.parseInt(inputString);
testnum++;
}while(testnum<=10);
average=(testScore+testScore+testScore+testScore+testScore+testScore+testScore+testScore+testScore+testScore)/10;
JOptionPane.showMessageDialog(null,"The lowest score is " + min(testScore));
JOptionPane.showMessageDialog(null, "The highest score is " +max(testScore));
JOptionPane.showMessageDialog(null, "The average score is " + average);
System.exit(0);
}
}
-
You'll find the min and max similar to how you'd manually do it with an array: have a min var that you'll compare the input to and if input is less, set = to input, and similarly for max (though you'll be checking if input value is greater than max of course). Also, you'll want a total value that you add as the values are input. Your testScore + testScore + .... will only add the last entered value ten times.
Keep on working at it. Much luck!
-
Crud, I didn't even see the error with finding the average. If I were to add another 9 lines and change testScore to testScore1 and so on. Would that work?