如何在这个 GPA 计算器中构建我的 for 循环?爪哇
Posted
技术标签:
【中文标题】如何在这个 GPA 计算器中构建我的 for 循环?爪哇【英文标题】:How can I build my for loop in this GPA Calculator? Java 【发布时间】: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
循环。
【参考方案1】:
我认为您正在尝试这样做: 循环 4 次并添加积分和学分进行平均
import java.util.Scanner;
class Prog
public static void main(String[] args)
String grade = "";
int creditHour = 0;
int creditHourSum = 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 (int i = 0; i < 4; i++)
System.out.print("Enter grade: ");
grade = scnr.nextLine();
System.out.print("Enter number of credits for grade: ");
creditHour = Integer.parseInt(scnr.nextLine());
creditHourSum += creditHour;
switch (grade)
case "A":
pointsPerCourse += 4 * creditHour;
break;
case "B":
pointsPerCourse += 3 * creditHour;
break;
case "C":
pointsPerCourse += 2 * creditHour;
break;
case "D":
pointsPerCourse += 1 * creditHour;
break;
default:
pointsPerCourse += 0 * creditHour;
System.out.println(pointsPerCourse);
gpa = (double) pointsPerCourse / creditHourSum;
System.out.println(pointsPerCourse + ", " + creditHourSum);
System.out.printf("The GPA is %.1f ", gpa);
scnr.close();
输出
This program will calculate pointsPerCourses based on course grades
Enter grade: A
Enter number of credits for grade: 4
16
Enter grade: E
Enter number of credits for grade: 4
16
Enter grade: B
Enter number of credits for grade: 4
28
Enter grade: C
Enter number of credits for grade: 3
34
34, 15
The GPA is 2.3
【讨论】:
哇,非常感谢!我绝对忘记了“+=”... @ryanmonty42 如果它适合你,你能接受答案吗?【参考方案2】:这不是 golang,你不能像这样使用 for
循环,并且你想把你的 switch case 放在循环中。有人建议使用 while
循环,但由于您只想循环 4 次,因此适当的 for
循环将为您完成。
for(int i = 0, i < 4, i++)
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;
【讨论】:
啊,我明白了,谢谢!我对在 for 循环中放入什么感到困惑,我认为我应该在其中放入某种字符串,但我必须坚持使用“int i”变量。以上是关于如何在这个 GPA 计算器中构建我的 for 循环?爪哇的主要内容,如果未能解决你的问题,请参考以下文章