Why is this Java code not working calculate average max and min? -
i supposed build program gets grades in class , calculates min , max , average ask if want continue. code used it's still not working. can see 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 grades int answer = 1; // declare variable answer =1 { // start of loop system.out.print("please enter name of course:"); // gives prompt enter name of course // goes next line string course=input.nextline(); // declare course string , sets value input system.out.println("please enter scores " + course + " on single line , type -1 @ end"); //prompts user enter scores class in 1 line , -1 stop int max=-100; // declare max integer = -100 int min=500;//declare min integer = 500 int sum=0;// declare sum intgetr = 0 int grade; // declare grade integer grade=input.nextint(); // set grade next user iput if (grade==-1) // if statement if sentinel value set stop break; list.add(grade); // adds grade grade variable linkedlist sum=sum+grade;// put value of sum sum , new grade if (grade>max) // if statement if grade more max sets max grade max=grade; if (grade<min) // if statement if grade less min sets min grade min=grade; system.out.println("the course name: "+ course); // prints message of course name entered system.out.println("number of scores :"+list.size()); // sits number of scores using size of list system.out.printf("the average score: %.2f" ,(double)sum/list.size()); // calculates average 2 decimal values using sum divided length of list system.out.println(); // goes next line system.out.println("the minimum score: "+min); // sets minimum value min system.out.println("the maximum score :"+max);// sets minimum value min answer = joptionpane.showconfirmdialog(null," continue?"); // sets value of variable answer confrim dialog box } while (answer == 0); // sets condition loop answer == o continue if yes selected } }
i think following lines incorrect:
int max=-100; // declare max integer = -100 int min=500;//declare min integer = 500
they should be:
int max= 500; // declare max integer = -100 int min=-100;//declare min integer = 500
also should moved outside do-while
.
Comments
Post a Comment