我又开始学习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);
}
}
你永远不会分配任何东西给'grade';你只需返回值并丢弃它。 – resueman