我正在制作一个简单的平均成绩计算器。基本上接受用户标记和该模块的百分比并显示平均百分比。该程序确实有效,但它在while循环中发生了一些故障。 一旦用户输入-1以下的任何值,while循环应该结束但是它会继续几次然后退出while循环。此外,它首先让用户输入一个数字,以确保启动while循环,然后输入文字' Enter Mark'出现,使用户再次输入他们的标记。我试图让while循环自动启动,但不知道是怎么回事。
import java.util.ArrayList;
import java.util.Scanner;
public class percentage {
static ArrayList<Double> marks = new ArrayList<>();
static ArrayList<Double> percentage = new ArrayList<>();
static Scanner input = new Scanner(System.in);
static void addingToMarks(double currentmark) {
marks.add(currentmark);
}
public static void main(String[] args) {
System.out
.println("Type in the number of marks you got \n"
+ "in the module. And then type the percentage weight of it.\n"
);
double exitLoop = input.nextDouble();
while (exitLoop > -1) {
System.out.println("Type in your marks");
marks.add(input.nextDouble());
System.out
.println("Type in the weighted percentage of the module: ");
percentage.add(input.nextDouble());
exitLoop = input.nextDouble();
}
System.out.println(percentage);
System.out.println(marks);
System.out.println("Your average percent for the module is: "
+ gradeCalculate());
}
static double gradeCalculate() {
double totalaverageweight = 0;
for (int x = 0; x < marks.size(); x++) {
totalaverageweight += ((marks.get(x) / 100) * percentage.get(x));
}
return totalaverageweight;
}
}
答案 0 :(得分:1)
我认为do ... while循环在这种情况下会起作用,因为测试条件将在循环结束时发生
do{
////your code goes here
}while(exitLoop!=-1);