如何在此 GPA 計算器中構建我的 for 回圈?全新的 Java,對于我們的班級,我們需要詢問用戶的等級和學分 4 次,然后計算他們的平均值。我們被指示使用 switch 陳述句和回圈,但沒有太多其他指令。我們剛剛了解了 for、while 和 do 回圈。這是我到目前為止所擁有的,不知道下一步該怎么做。
這就是輸出應該是什么樣子。
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();
}
uj5u.com熱心網友回復:
這不是 golang,你不能使用這樣的for回圈,而且你想把你的開關盒放在回圈內。有人建議了一個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;
}
}
uj5u.com熱心網友回復:
我認為您正在嘗試這樣做:回圈 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343079.html
上一篇:回圈中具有零的所有陣列組合
