I am supposed to build a program that gets the grades in a class and calculates min and max and average then ask if you want to continue. This is the code I used but it's still not working. Can you see where the mistake is?
public class calculateAvgMAxMin
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // declares scanner input
LinkedList<Integer> list = new LinkedList<Integer>(); // declare new LinkedList of variables which are the grades
int answer = 1; // declare variable answer =1
do { // start of do loop
System.out.print("Please enter the name of the course:"); // gives a prompt to enter the name of the course
// goes to the next line
String course=input.nextLine(); // declare course as string and sets the value as the input
System.out.println("Please enter the scores for " + course + " on a single line and type a -1 at the end"); //prompts user to enter scores for class in one line and -1 to stop
int max=-100; // declare max as integer = -100
int min=500;//declare min as integer = 500
int sum=0;// declare sum as intgetr = 0
int grade; // declare grade as integer
grade=input.nextInt(); // set grade as next user iput
if (grade==-1) // if statement if the sentinel value is set then stop
break;
list.add(grade); // adds the grade from the grade variable to the LinkedList
sum=sum+grade;// put the value of sum to sum and the new grade
if (grade>max) // if statement if grade is more than the max then sets max as the grade
max=grade;
if (grade<min) // if statement if grade is less than the min then sets min as the grade
min=grade;
System.out.println("The course name: "+ course); // prints message of the course name entered
System.out.println("Number of Scores :"+list.size()); // sits the number of scores using the size of the list
System.out.printf("The Average Score: %.2f" ,(double)sum/list.size()); // calculates average to 2 decimal values using the sum divided by the length of the list
System.out.println(); // goes to next line
System.out.println("The Minimum Score: "+min); // sets the minimum value to min
System.out.println("The Maximum Score :"+max);// sets the minimum value to min
answer = JOptionPane.showConfirmDialog(null," Would you like to continue?"); // sets the value of the variable answer to confrim dialog box
} while (answer == 0); // sets the condition for do loop to answer == o so that it would continue only if the yes is selected
}
}
I think the following lines are incorrect:
int max=-100; // declare max as integer = -100
int min=500;//declare min as integer = 500
They should be:
int max= 500; // declare max as integer = -100
int min=-100;//declare min as integer = 500
Also they should be moved outside do-while
.
You need another do..while for each score
int grade; // declare grade as integer
do{
grade=input.nextInt(); // set grade as next user iput
if(grade==-1) // if statement if the sentinel value is set then stop
break ;
list.add(grade); // adds the grade from the grade variable to the LinkedList
sum=sum+grade;// put the value of sum to sum and the new grade
if(grade>max) // if statement if grade is more than the max then sets max as the grade
max=grade;
if(grade<min) // if statement if grade is less than the min then sets min as the grade
min=grade;
} while (true);
I would start by programming to the List
interface (instead of the concrete collection) and I would prefer the diamond operator <>
(available Java 7+). Next, I would initialize max
and min
to Integer.MIN_VALUE
and Integer.MAX_VALUE
respectively (because no int
is smaller than the first or, it follows, larger than the second). Then I'd prefer Math.max(int, int)
and Math.min(int, int)
. Finally, I suggest you get all the values before you display statistics. Something like,
List<Integer> list = new LinkedList<>();
int answer = 1;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
do {
System.out.print("Please enter the name of the course:");
String course = input.nextLine();
System.out.printf("Please enter the scores for %s on a single "
+ "line and type a -1 at the end%n", course);
int grade = input.nextInt();
if (grade == -1) {
break;
}
System.out.printf("Course %s, Grade %d%n", course, grade);
list.add(grade);
sum += grade;
max = Math.max(max, grade);
min = Math.min(min, grade);
answer = JOptionPane.showConfirmDialog(null,
"Would you like to continue?");
} while (answer == 0);
System.out.println("Number of Scores :" + list.size());
System.out.printf("The Average Score: %.2f%n",
sum / (double) list.size());
System.out.println("The Minimum Score: " + min);
System.out.println("The Maximum Score :" + max);
It looks like an error with where and what you're setting the max and min values to. It should be:
int min = -100;
int max = 500;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.