【发布时间】:2021-12-14 01:22:42
【问题描述】:
如何在这个 GPA 计算器中构建我的 for 循环?刚接触Java,我们班需要4次询问用户的成绩和学分,然后计算他们的平均值。我们被指示使用 switch 语句和循环,但没有太多其他指令。我们刚刚了解了 for、while 和 do 循环。这是我目前所拥有的,不知道下一步该做什么。
This is what output is supposed to look like.
public static void main(String[] args) {
String grade = "";
int creditHour = 0;
int pointsPerCourse = 0;
double gpa = 0.0;
Scanner scnr = new Scanner(System.in);
System.out.println("\t\t\tpointsPerCourse Calculator");
System.out.println("This program will calculate pointsPerCourses based on course grades");
for ();
{
System.out.print("Enter grade: ");
grade = scnr.nextLine();
System.out.print("Enter number of credits for grade: ");
creditHour = Integer.parseInt(scnr.nextLine());
}
switch (grade) {
case "A":
pointsPerCourse = 4 * creditHour;
case "B":
pointsPerCourse = 3 * creditHour;
case "C":
pointsPerCourse = 2 * creditHour;
case "D":
pointsPerCourse = 1 * creditHour;
default:
pointsPerCourse = 0 * creditHour;
}
gpa = (double) pointsPerCourse / creditHour;
System.out.printf("The GPA is %.1f ", gpa);
scnr.close();
}
【问题讨论】:
-
使用
Scanner,您可能需要while循环。
标签: java loops switch-statement gpa