2012-11-09 78 views
-2

我需要幫助完成作業。我需要編寫課程程序,將成績轉換成成績。如果等級與A +一樣,則將等級點增加0.3,並且 - 將減少0.3。需要作業想法

private static final double GradePoint = 0; 
private static Scanner input; 

public static void main(String [] args) 
{ 
    String grade ;  
    double GradePoint = 0; 

    System.out.print("Please enter your grade: "); 
    input = new Scanner(System.in); 
    grade = input.nextLine(); 

    switch(grade) 
    { 
     case "A": 
     case "a": GradePoint = 4; break; 
     case "B": 
     case "b": GradePoint = 3; break; 
     case "C": 
     case "c": GradePoint = 2; break; 
     case "D": 
     case "d": GradePoint = 1; break; 
     case "F": 
     case "f": GradePoint = 0; break; 
    }  

    System.out.print("Your grade is: "+GradePoint); 
} 

public double getGradePoint(String grade) 
{ 
    return GradePoint; 

} 

我不明白的是如何使用該方法來計算。我仍然是初學者。

+1

你的問題是什麼? –

+0

@Mohd Khairul:只需檢查第二個字母。你有方法將字符串轉換爲數組。並檢查第二個字符匹配+或 - 不是你想要什麼? –

+0

作業標籤?另外,要搜索「+」,您可以使用這種名爲indexOf的方法。例如:if(str1.indexOf(「#」)> -1){//#在str1中找到}。 –

回答

0

我很無聊,有什麼好做所以這裏:)

值工作
public class GradeCalculator 
{ 

    public static void main(String[] args) 
    { 
     System.out.print("Please enter your grade: "); 
     Scanner input = new Scanner(System.in); 
     String grade = input.nextLine().trim(); 

     GradeCalculator calculator = new GradeCalculator(); 

     double gradePoint = calculator.getGradePoint(grade); 
     System.out.print("Your grade is: " + gradePoint); 
    } 

    private double getGradePoint(String grade) 
    { 
     int score = getGradeScore(grade.charAt(0)); 

     double modifier = 0; 

     if (grade.length() > 1) 
     { 
      modifier = getModifierValue(grade.charAt(1)); 
     } 

     return score + modifier; 
    } 

    private int getGradeScore(char grade) 
    { 
     int score = 0; 

     switch (grade) 
     { 
      case 'A': 
      case 'a': 
       score = 4; 
       break; 
      case 'B': 
      case 'b': 
       score = 3; 
       break; 
      case 'C': 
      case 'c': 
       score = 2; 
       break; 
      case 'D': 
      case 'd': 
       score = 1; 
       break; 
      case 'F': 
      case 'f': 
       score = 0; 
       break; 
     } 

     return score; 
    } 

    private double getModifierValue(char modifier) 
    { 
     double value = 0; 

     switch (modifier) 
     { 
      case '+': 
       value = 0.3; 
       break; 
      case '-': 
       value = -0.3; 
       break; 
     } 

     return value; 
    } 
} 
+0

這對我幫助最大。也許我需要修改一些代碼。像A =必須是結果4.0而不是4.3。感謝我的朋友。 – Beginner

1
I have to use CLASS and method*public double getGradePoint(String grade)* to 
return the grade point of grade entered. 

您需要將整個代碼從main()移到getGradePoint(String grade);

也是你的開關情況switch(grade)不會像「A +」,因爲是匹配字符串沒有這樣的情況下,「A +」

+0

那麼在這個程序中像A +一樣嗎?也許使用任何類型的計算。 – Beginner