2017-03-10 123 views
-3

我又開始學習Java了。我遇到顯示char數據類型返回值的問題。一切工作都期待這一個。它應該能夠將等級顯示爲A B C D,但它是空白的。有人可以幫我找到錯誤並解決問題嗎?有人建議我可以使用System.out.println(「A」);在返回之前A.我試過了,但沒有奏效。我在SF中搜索,但找不到確切問題的解決方案。先謝謝你。爲什麼它不返回返回類型字符?

package score; 

import java.util.Scanner; 

/** 
* 
* @author rabin 
*/ 
public class Score { 
    static double earnedPoints, possiblePoints, pointsPercent; 
    static char grade; 
    //static String name; 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     int testNumber; 

     String stuName; 
     // TODO code application logic here 
     System.out.println("How many tests were given?"); 
     Scanner input1 = new Scanner(System.in); 
     testNumber = input1.nextInt(); 

     System.out.println("Enter Student's Name"); 
     Scanner nameInput = new Scanner(System.in); 
     stuName = nameInput.next(); 
     if (stuName.contains(" ")){ 
      System.out.println("This field should not contain spaces"); 


     } 
     pointsEarned(testNumber); 
     letterGrade(pointsPercent); 
     displayResult(stuName, pointsPercent, grade); 
    } 
    public static double pointsEarned(int testNumber){ 

     System.out.println("Enter Points Earned."); 
     Scanner pointsInput = new Scanner(System.in); 
     earnedPoints = pointsInput.nextDouble(); 
     System.out.println("Enter Possible Points."); 
     Scanner possibleInput = new Scanner(System.in); 
     possiblePoints = possibleInput.nextDouble(); 
     pointsPercent = ((earnedPoints/possiblePoints) * 100); 
     return pointsPercent; 
    } 
    public static char letterGrade(double percentage){ 
     if (percentage >= 90 && percentage <= 100){ 
      System.out.println("A"); 
      return 'A'; 
     } 
     else if (percentage >= 80 && percentage < 90){ 
      System.out.println("B"); 
      return 'B'; 
     } 
     else if (percentage >= 70 && percentage < 80){ 
      System.out.println("C"); 
      return 'C'; 
     } 
     else if (percentage >= 60 && percentage < 70){ 
      System.out.println("D"); 
      return 'D'; 
     } 
     else { 
      System.out.println("F"); 
      return 'F'; 
     } 


    } 
    public static void displayResult(String name, double percentage, char grade){ 
     System.out.println("Result"); 
     System.out.println("Name: " +name); 
     System.out.println("Percent: " +percentage); 
     System.out.println("Letter Grade: " +grade); 



    } 

} 
+1

你永遠不會分配任何東西給'grade';你只需返回值並丟棄它。 – resueman

回答

1

的問題是,你調用適當的方法,但你不能把它分配給grade變量。

返回的值賦給變量grade象下面這樣:

grade = letterGrade(pointsPercent); 
3

你不把在年級變量返回的結果,你必須使用:

grade = letterGrade(pointsPercent); 
0

main程序,你需要使用您的static變量來保存您方法的return值:

public static void main(String[] args) { 
     int testNumber; 
     String stuName; 
     // TODO code application logic here 
     System.out.println("How many tests were given?"); 
     Scanner input = new Scanner(System.in); 
     testNumber = input.nextInt(); 

     System.out.println("Enter Student's Name"); 
     //Scanner nameInput = new Scanner(System.in); you don't have to use another scanner variable to read inputs only one is enough 
     stuName = input.next(); 
     if (stuName.contains(" ")){ 
      System.out.println("This field should not contain spaces"); 


     } 
     pointsPercent = pointsEarned(testNumber); // save the return value of pointsEarned method in pointsPrecent 
     grade = letterGrade(pointsPercent); //save the return value of letterGrade method in grade 
     displayResult(stuName, pointsPercent, grade); 
    } 
相關問題