我希望有人可以在这里查看我的代码并告诉我为什么在第88行(public static double dblLetterToGrade(String strLetter))它给了我一个错误(如标题中所述)。我是Java的新手,所以我可能犯了一个愚蠢的错误而没有意识到它并且可能给我一个修复:
import javax.swing.JOptionPane;
import java.io.FileWriter;
import java.io.*;
import java.io.PrintWriter;
public class GPAProject
{
static String strReport, strName, strCreditsString, strClass, strGradeString, strLetter;
static double dblLetterToGrade, dblTotalWeightedGrades = 0, dblGradeVaule=-1;
static int intTotalCredits = 0, intCreditsValue = -1;
static boolean boolfin = false;
public static void main(String[] args) throws IOException
{
// Ask for name and write the output header which strReport gathers the data for the output file.
strReport = "Classes for " + strName + ":\n";
strReport += "Title\t\t\t\tCredits\t\t\t\tGrade\n";
// Compute GPA (if there are classes).
strReport += "\nGPA: ";
if (intTotalCredits > 0)
{
strReport += String.format("%.2f", dblTotalWeightedGrades / intTotalCredits);
}
else
{
strReport += "None (no classes).";
}
// Write the report to classes.txt file.
PrintWriter output = new PrintWriter(new FileWriter("classes.txt"));
output.println(strReport);
output.close();
JOptionPane.showMessageDialog(null, "The report was written to the file.");
{
// Asks the user for the name until a valid name is entered.
while (strName == null)
{
strName = JOptionPane.showInputDialog("Enter your name:");
}
// Allows the user to enter class data until there are no more.
while (!boolfin)
{
String strClass = JOptionPane.showInputDialog("Enter class name (Cancel to finish):");
if (strClass == null)
{
boolfin = true;
}
else
{
// Ask for grade.
while (dblGradeVaule == -1)
{
strGradeString = JOptionPane.showInputDialog("Enter grade (uppercase):");
if (strGradeString == null)
{
JOptionPane.showMessageDialog(null, "Please enter a grade.");
}
else {
dblGradeVaule = dblLetterToGrade(strLetter);
if (dblGradeVaule == -1) {
JOptionPane.showMessageDialog(null, "The grade is invalid. Please enter it again.");
}
}
}
}
}
}
}
// Converts letter grade to numeric value
public static double dblLetterToGrade(String strLetter)
{
double dblGrade=0;
switch (strLetter)
{
case "A": dblGrade = 4.00; break;
case "A-": dblGrade = 3.7; break;
case "B+": dblGrade = 3.2; break;
case "B": dblGrade = 3.0; break;
case "B-": dblGrade = 2.7; break;
case "C+": dblGrade = 2.2; break;
case "C": dblGrade = 2.0; break;
case "D+": dblGrade = 1.2; break;
case "D": dblGrade = 1.00; break;
case "D-": dblGrade = 0.7; break;
case "F": dblGrade = 0; break;
}
{
// Ask for credit hours.
while (intCreditsValue == -1)
{
strCreditsString = JOptionPane.showInputDialog("Enter credit hours:");
intCreditsValue = Integer.parseInt(strCreditsString);
if (intCreditsValue <= 0) ;
{
JOptionPane.showMessageDialog(null, "Please enter a positive number.");
}
}
}
{
// Add to running totals and write to output string in file.
intTotalCredits += intCreditsValue;
dblTotalWeightedGrades += (intCreditsValue * dblGradeVaule);
strReport += strClass + "\t\t\t\t" + intCreditsValue + "\t\t\t\t" + strGradeString + "\n";
}
}
}
&#13;
答案 0 :(得分:0)
您必须在dblLetterToGrade(String strLetter)方法的末尾返回一个double值,因为该方法具有double return type。
// Converts letter grade to numeric value
public static double dblLetterToGrade(String strLetter)
{
double dblGrade=0;
switch (strLetter)
{
case "A": dblGrade = 4.00; break;
case "A-": dblGrade = 3.7; break;
case "B+": dblGrade = 3.2; break;
case "B": dblGrade = 3.0; break;
case "B-": dblGrade = 2.7; break;
case "C+": dblGrade = 2.2; break;
case "C": dblGrade = 2.0; break;
case "D+": dblGrade = 1.2; break;
case "D": dblGrade = 1.00; break;
case "D-": dblGrade = 0.7; break;
case "F": dblGrade = 0; break;
}
return dblGrade; // returns the value because the method has a double return type.
}
&#13;