2017-03-10 94 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); 
    } 
相关问题