public class GpaTestEx2
{
public static void main (String[] args)
{
//declarations
Scanner in = new Scanner(System.in); //input object
int numCourses; //number of courses - can be changed
int credits; //number of credits for a course
String grade; //grade for course
//read in number of courses
System.out.print("Enter number of courses: ");
numCourses = in.nextInt();
//create Gpa object to hold specified number of courses
Gpa myGPA = new Gpa(numCourses);
//read in all courses and add course information to Gpa object
for (int k=0; k<numCourses; k++)
{
System.out.print("Enter credits for course " + (k+1) + ": ");
credits = in.nextInt();
System.out.print("Enter grade for course " + (k+1) + ": ");
grade = in.next();
myGPA.addCourse(credits, grade);
}
//print results
System.out.println();
System.out.printf("GPA is %4.2f%n", myGPA.calcGPA());
} //end main
}
這是我不被允許改變的類。基本上我需要找到一個GPA使用2個陣列credits[]
和grades[]
,其中第一個是int
,第二個是String
。我需要使用方法addCourse
(需要兩個參數:newCredits
(課程學分數)和newGrade
- 課程成績爲String
(僅限一個字母))。沒有返回值,因爲假設數組中有空間可以添加課程。 addCourse
info:在信用數組的第一個可用位置存儲newCredits
。用數組計算gpa(字符串到int運行錯誤)
將newGrade存儲在等級數組中的相同位置。 遞增numCourses以指示用戶已經輸入了一個課程。
calcGpa
(無參數,返回GPA爲雙) - 計算並返回基於A = 4分標準GPA,B = 3,C = 2,d = 1,F = 0。如果無法計算GPA,則返回0.0。
構造函數:noCourses - 最大課程數。沒有返回類型。
更多信息:爲參數大小的兩個數組分配空間。設置maxCourses到noCourses.Sets的numCourses零
我的代碼:GPA類需要返工:
import java.util.*;
public class Gpa
{
Scanner in = new Scanner(System.in);
int numCourses;
int maxCourses;
int [ ] credits = new int [maxCourses];
String [ ] grades = new String [maxCourses];
int total = 0;
int totalSum = 0;
public Gpa(int noCourses)
{
maxCourses = noCourses;
numCourses = 0;
}
public void addCourse(int newCredits, String newGrade)
{
for(int i = 0; i < numCourses; i++)
{
credits[i] = newCredits;
}
for(int i = 0; i < numCourses; i++)
{
grades[i] = newGrade;
}
switch(newGrade)
{
case "A": newGrade = 4;
break;
case "a": newGrade = 4;
break;
case "B": newGrade = 3;
break;
case "b": newGrade = 3;
break;
case "C": newGrade = 2;
break;
case "c": newGrade = 2;
break;
case "D": newGrade = 1;
break;
case "d": newGrade = 1;
break;
case "F": newGrade = 0;
break;
case "f": newGrade = 0;
break;
}
totalSum = totalSum + (newCredits * newGrade);
numCourses++;
}
public double calcGPA()
{
for (int x : credits)
{
total+=x;
}
double gpa = (double)totalSum/total;
return gpa;
}
}
我得到詮釋的錯誤不能被轉換成字符串。而且我需要將這個新的等級從(A,B,C,D,F ..)等級的字符串轉換爲等級所代表的信用數字。它在這種情況下工作:在我的第一部分與char,我試圖看看我的第一部分,它工作正常,我注意到,字符更容易轉換爲int,基本上我不需要轉換它但String,它doesn以這種方式工作,我試圖谷歌它,但我找不到合適的解決方案。我需要在這部分中迷失方向的指導。這是工作的一部分:第1部分:
import java.util.*;
public class GpaEx1
{
public static final int NUMBER_OF_COURSES = 4;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int [ ] credits = new int [NUMBER_OF_COURSES];
char [ ] grades = new char [NUMBER_OF_COURSES];
int total = 0;
int totalSum = 0;
for(int i = 0; i < NUMBER_OF_COURSES; i++)
{
System.out.println("Enter credits for course " + (i+1) + ": ");
int credit = in.nextInt();
credits[i] = credit;
System.out.println("Enter grade for course " + (i+1) + ": ");
char grade = in.next().charAt(0);
grades[i] = grade;
switch(grade)
{
case 'A': grade = 4;
break;
case 'a': grade = 4;
break;
case 'B': grade = 3;
break;
case 'b': grade = 3;
break;
case 'C': grade = 2;
break;
case 'c': grade = 2;
break;
case 'D': grade = 1;
break;
case 'd': grade = 1;
break;
case 'F': grade = 0;
break;
case 'f': grade = 0;
break;
}
totalSum = totalSum + (credit * grade);
}
for (int x : credits)
{
total+=x;
}
System.out.println("Total number of credits: " + total);
System.out.println("Total number of points: " + totalSum);
double gpa = (double)totalSum/total;
System.out.printf("Gpa: %.2f", gpa);
}
}
哪條線給你錯誤,那是什麼錯誤? –
你用一個char乘以一個int。您需要將等級設爲數值數據類型 – Turtle
基本上所有開關(newGrade)都會給我提供錯誤,尤其是在newGrade = 4時;或任何數字它說int不能轉換爲字符串:totalSum = totalSum +(newCredits * newGrade); ^ 第一種類型:int 第二種類型:字符串 11錯誤 – OneTwoThree