Can anyone compile this code and help me because it's not returning the results I want. I don't know if the return is not working or what, please help me, here is the code:
public class ComputeAverage {
public static void main (String[] args) {
new ComputeAverage().computeAverage(4);
}
public double computeAverage(int how_many)
{ double total_points = 0.0; // the total of all test scores
int count = 0; // the quantity of tests read so far
while ( count != how_many )
// at each iteration: total_points == exam_1 + exam_2 + ... + exam_count
{ // ask the user for the next exam score:
String input = JOptionPane.showInputDialog("Type next exam score:");
int score = new Integer(input).intValue();
if (score < 0) {
JOptionPane.showMessageDialog(null, "Enter a positive numer");
total_points = total_points - score;
}
// add it to the total:
total_points = total_points + score;
count = count + 1;
// print a summary of the progress:
System.out.println("count = " + count + "; total = " + total_points);
}
// at conclusion: total_points == exam_1 + exam_2 + ... + exam_how_many
return (total_points / how_many);
}
}
You are returning the value back to the main but you are not printing it anywhere. you can use the alert box in swings to show the result.
In main function instead of.,
public static void main (String[] args) {
new ComputeAverage().computeAverage(4);
}
try:
public static void main (String[] args) {
JOptionPane.showMessageDialog(null,"Exam score : " + new ComputeAverage().computeAverage(4));
}
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.