如何在此 GPA 计算器中构建我的 for 循环? Java
How can I build my for loop in this GPA Calculator? Java
如何在此 GPA 计算器中构建我的 for 循环? Java 是全新的,对于我们的 class,我们需要询问用户的成绩和学时 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();
}
这不是 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;
}
}
我认为您正在尝试这样做:
循环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
如何在此 GPA 计算器中构建我的 for 循环? Java 是全新的,对于我们的 class,我们需要询问用户的成绩和学时 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();
}
这不是 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;
}
}
我认为您正在尝试这样做: 循环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