我又開始學習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