我正在尝试制作一个平均计算器,但使用实际的加权成绩上课。我做了一个数组列表,可以存储无限数量的双精度数,但是我不知道如何找到或使用它们。我对此并不陌生,我不希望有人为我做这件事,我希望他们解释一下并举例说明如何找到它并实现它。谢谢
代码如下:
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
public class Averager{
static ArrayList <Double> array = new ArrayList <Double>();
public static void main (String[] args){
Scanner input = new Scanner(System.in);
double total = 0;
int grades;
int score;
int counter = 0;
double average;
String type;
type = JOptionPane.showInputDialog ("Which averager do you want to use, weighted or normal?");
if (type.equalsIgnoreCase("Normal")){
System.out.println ("How many scores do you want to average?");
grades = input.nextInt();
System.out.println ("What are your scores?");
while (counter < grades){
score = input.nextInt();
total = total + score;
counter++;
}
average = total / grades;
average = Math.round (average * 100.0) /100.0;
System.out.println ("The average is " + average);
}else if (type.equalsIgnoreCase ("Weighted")){
// this is the part where i need help
System.out.println ("Enter the weights of the course, type 0 when finished");
double weight = input.nextDouble();
while (weight != 0){
array.add(weight);
weight = input.nextDouble();
}
for (double i : array){ //i was trying to follow a tutorial but this just outputs all of the variable where as i need section for specific ones
System.out.println (i);
}
}else{
System.out.println ("Error 101, can't do that yet");
}
}
}