I need help making a java program that will allow me to input 10 test grades into an array using a dialog box in one method. Then in a different method it needs to display the grades and calculate the average, highest, and lowest grade. I have no idea where to start and need a lot of help please.
Try getting this done in a console based Java Program and once you have the logic done, you can always get it in the dialog box.
Try reading Java How to Program by Dietel. There's a similar code there for computing average tests...
Hey, try this one using loop : Code: double total; for(int student = 1; student <= 4; student++) { System.out.printf("Student %d\n", student); double sum = 0, count = 0; while(true) { System.out.printf("Enter your score: "); double input = scanner.nextDouble(); if(input == -1) break; sum += input; count++; } total += sum; System.out.printf("Average: %.2f\n", sum / count); } System.out.printf("Total: %.2f\n", total);
check this out: Code: /* * java program that will allow me to input 10 test grades into an array using a dialog box in one method. * Then in a different method it needs to display the grades and calculate the average, highest, and lowest grade */ import javax.swing.JOptionPane; public class input10testgrades { private final int studentGrades[] = new int[10]; private double average; public void inputStudentGrades() { for(int i = 0; i < studentGrades.length; i++) { studentGrades[i] = Integer.parseInt(JOptionPane.showInputDialog(null)); } } public void displayGrade() { System.out.println("This are all the test grades\n================================="); for(int i = 0; i < studentGrades.length; i++) { System.out.println(studentGrades[i]); } System.out.println("\n\nThe total average of grades\n================================="); double gradesSum = 0; int highestNum = 0; int lowestNum = studentGrades[0]; //here we are getting the first grade test for(int grade : studentGrades) { gradesSum+=(double)grade; //here we are getting the total sum of all the test grade //here am checking for the highest number if(grade > highestNum) highestNum = grade; //here am checking for the lowest number if(grade < lowestNum) lowestNum = grade; } average = gradesSum/10; //here we are calculate the average of the total grade test System.out.println(average); System.out.println("\nThe Highest Grade Number is :"+highestNum); System.out.println("\nThe Lowest Grade Number is :"+lowestNum); } public static void main(String []grade) { input10testgrades testgrades = new input10testgrades(); testgrades.inputStudentGrades(); testgrades.displayGrade(); } }
If this is your first introduction to Java I would suggest ignoring the fact you want a dialog box and make a simple console application to begin with. Once this is working correctly you can then put this into a GUI.
Code: import java.util.Scanner; public class AverageTestScore { public static void main(String[] args) { //create the variables int TestScore1; int TestScore2; int TestScore3; int TestScore4; int TestScore5; float AverageScore; //create keyboard for input Scanner Keyboard = new Scanner(System.in); //Ask the user for the 5 test scores System.out.println("Please enter test score 1"); TestScore1 = Keyboard.nextInt(); System.out.println("Please enter test score 2"); TestScore2 = Keyboard.nextInt(); System.out.println("Please enter test score 3"); TestScore3 = Keyboard.nextInt(); System.out.println("Please enter test score 4"); TestScore4 = Keyboard.nextInt(); System.out.println("Please enter test score 5"); TestScore5 = Keyboard.nextInt(); //Calculate the average score for the tests AverageScore = ((TestScore1 + TestScore2 + TestScore3 + TestScore4 + TestScore5)/5); //Display Average test Score System.out.println("The average score of the 5 tests is " + AverageScore); } }