my problem is in the last part of the program i can\'t get the grades entered in
ID: 3641113 • Letter: M
Question
my problem is in the last part of the program i can't get the grades entered in the array to print.import javax.swing.JOptionPane;
public class Average{
public static void main(String[] args) {
String input, amount;
double data[];
int total;
double sum = 0, average = 0;
System.out.println(" Average Program");
amount = JOptionPane.showInputDialog("Enter Total Number of grades to enter");
total = Integer.parseInt(amount);
data = new double[total];
for (int i = 0; i < data.length; i++) {
input = JOptionPane.showInputDialog("Enter Your grades");
data[i] = Double.parseDouble(input);
}
for (int i = 0; i < data.length; i++) {
sum += data[i];
average = sum / data.length;
}
JOptionPane.showMessageDialog(null, "The Grades entered are: " + data[]
+ " The Average is: " + average, "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}
Explanation / Answer
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Average {
public static void main(String[] args) {
String input, amount;
double data[];
int total;
double sum = 0, average = 0;
String gradesReport;
DecimalFormat gradeFormat = new DecimalFormat("#.00");
System.out.println(" Average Program");
amount = JOptionPane.showInputDialog("Enter Total Number of grades to enter");
total = Integer.parseInt(amount);
data = new double[total];
for(int i = 0; i < data.length; i++) {
input = JOptionPane.showInputDialog("Enter Your grades");
data[i] = Double.parseDouble(input);
}
for (int i = 0; i < data.length; i++) {
sum += data[i];
average = sum / data.length;
}
gradesReport = "The Grades entered are: ";
for (int i = 0; i < data.length; i++) {
gradesReport += gradeFormat.format(data[i]) + " ";
}
gradesReport += " The Average is: " + gradeFormat.format(average);
JOptionPane.showMessageDialog(null, gradesReport,
"Result", JOptionPane.INFORMATION_MESSAGE);
}
}
(Red: optional)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.