我很陌生。我确定错误与我的数组有关。任何建议将不胜感激。当我运行它时,它允许我输入字母等级和学分时间,但我没有得到任何输出。
import java.util.Scanner;
/**
* Calculating gpa
*
* @author Brooklyn
* @version1 11/15/2016
*
*/
public class CalculateGPA {
/**
*
* Entry point for the program javadoc
*
*/
public static void main(String[] args) {
// declare vars
// input: prompt user to enter letter grade and credit hours
System.out.println("What are your letter grades?");
Scanner input = new Scanner(System.in);
String x = input.nextLine();
System.out.println("How many credit hours are you taking?");
int v = input.nextInt();
int[] creditHours = new int[v];
for (int i = 0; i < creditHours.length; i++) {
creditHours[i] = input.nextInt();
}
char[] letterGrade = x.toCharArray();
for (int i = 0; i < letterGrade.length; i++) {
letterGrade[i] = input.next().charAt(i);
}
// Processing: calculate gpa
double gpa = calculateGpa(letterGrade, creditHours);
}
// output: display GPA
// displayGPA(gpa);
/** Method calculates GPA */
public static double calculateGpa(char[] letterGrade, int[] creditHours) {
int totalQuality = 0;
int numericGrade;
double gpa = 0.0;
// double qualityPoint = 4 * creditHours[0];
if (letterGrade.equals("A")) {
numericGrade = 4;
totalQuality += numericGrade;
} else if (letterGrade.equals("B")) {
numericGrade = 3;
totalQuality += numericGrade;
} else if (letterGrade.equals("C")) {
numericGrade = 2;
totalQuality += numericGrade;
} else if (letterGrade.equals("D")) {
numericGrade = 1;
totalQuality += numericGrade;
} else if (letterGrade.equals("F")) {
numericGrade = 0;
totalQuality += numericGrade;
}
int totalHours = creditHours[0];
gpa = totalQuality / totalHours;
return totalQuality / totalHours;
}
public static void displayGPA(double gpa) {
System.out.println("Overall GPA is" + gpa);
}
}
答案 0 :(得分:0)
在main方法中调用此函数。 displayGPA(&#34; GPA Is&#34; + gpa);
答案 1 :(得分:0)
为了您的理解,我修改了您正确计算GPA的现有代码。
我的代码考虑了与成绩相关的学分。
比如你说A当然有1个学分,而B当然有5学分。
因此,您的GPA将用于每门课程(学分小时*成绩值)/(学分)
在这种情况下,(1 * 4 + 5 * 3) / (1 + 5) = 3.16
import java.util.Scanner;
public class CalculateGPA {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// get number of courses
System.out.println("How many courses have you taken ?");
int n = input.nextInt();
// get grades for those courses
char letterGrade[] = new char[n];
for (int i = 0; i < n; i++) {
System.out.println("What are your letter grades for course " + (i + 1));
String g = input.next();
letterGrade[i] = g.charAt(0);
}
// get credit hours associated with those courses
int creditHours[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.println(
"Enter credit hours for your course number: " + (i + 1) + " where you got " + letterGrade[i]);
creditHours[i] = input.nextInt();
}
// Processing: calculate gpa
double gpa = calculateGpa(letterGrade, creditHours);
// output: display GPA
displayGPA(gpa);
}
/** Method calculates GPA */
public static double calculateGpa(char[] letterGrade, int[] creditHours) {
// formula: (gradeValue1 * creditHours1 + gradeValue2 * creditHours2 + ... ) / (creditHours1 + creditHours2 + ...)
int totalQuality = 0;
int creditHoursTotal = 0;
double gpa = 0.0;
for (int i = 0; i < letterGrade.length; i++) {
switch (letterGrade[i]) {
case 'A':
totalQuality += 4 * creditHours[i];
creditHoursTotal += creditHours[i];
break;
case 'B':
totalQuality += 3 * creditHours[i];
creditHoursTotal += creditHours[i];
break;
case 'C':
totalQuality += 2 * creditHours[i];
creditHoursTotal += creditHours[i];
break;
case 'D':
totalQuality += 1 * creditHours[i];
creditHoursTotal += creditHours[i];
break;
case 'F':
// zero score and so there will be no impact on totalQuality
break;
default:
System.out.println("Invalid letter");
break;
}
}
gpa = (double) totalQuality / creditHoursTotal;
return gpa;
}
public static void displayGPA(double gpa) {
System.out.println("Overall GPA = " + gpa);
}
}
样品运行
你上过几门课程?
2
课程1的你的成绩是什么? 一个
课程2的你的成绩是什么? 乙
输入您的课程编号的学分:1,您获得A
1
输入您的课程编号的学分:2你得到B
5
总体GPA = 3.1666666666666665