0
我希望有人可以看看我的代码在这里告诉我为什么在第88行(公共静态双重dblLetterToGrade(字符串strLetter))它给了我一个错误在标题中)。我是新来的Java,所以我可能已经做出了愚蠢的错误,并没有意识到这一点,并可能给我的解决办法:public static double这个方法必须返回一个双精度值
import javax.swing.JOptionPane;
import java.io.FileWriter;
import java.io.*;
import java.io.PrintWriter;
public class GPAProject
{
\t
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;
\t public static void main(String[] args) throws IOException
\t
\t {
// Ask for name and write the output header which strReport gathers the data for the output file.
\t \t strReport = "Classes for " + strName + ":\n";
\t \t strReport += "Title\t\t\t\tCredits\t\t\t\tGrade\n";
// Compute GPA (if there are classes).
strReport += "\nGPA: ";
if (intTotalCredits > 0)
{
\t strReport += String.format("%.2f", dblTotalWeightedGrades/intTotalCredits);
}
else
{
\t 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.");
{
\t // Asks the user for the name until a valid name is entered.
\t \t while (strName == null)
\t
{
strName = JOptionPane.showInputDialog("Enter your name:");
}
\t \t // 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)
{
\t strGradeString = JOptionPane.showInputDialog("Enter grade (uppercase):");
if (strGradeString == null)
{
JOptionPane.showMessageDialog(null, "Please enter a grade.");
}
else {
\t dblGradeVaule = dblLetterToGrade(strLetter);
if (dblGradeVaule == -1) {
JOptionPane.showMessageDialog(null, "The grade is invalid. Please enter it again.");
}
}
}
}
}
}
}
\t \t \t \t \t // Converts letter grade to numeric value
public static double dblLetterToGrade(String strLetter)
{
\t
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;
}
{
\t // Ask for credit hours.
while (intCreditsValue == -1)
{
\t strCreditsString = JOptionPane.showInputDialog("Enter credit hours:");
\t intCreditsValue = Integer.parseInt(strCreditsString);
if (intCreditsValue <= 0) ;
{
JOptionPane.showMessageDialog(null, "Please enter a positive number."); \t \t
}
}
}
{
\t // 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";
}
\t }
}
您需要'在方法结束return'双。 –
你没有回报任何东西。但是因为你的方法装饰是'public static double',它期望方法返回一个'double' – CraigR8806
'public static double'表明你的方法是公共的,类特定的,并且返回一个double。在你的方法没有任何返回的地方,它不能是一个'double'方法,应该变成一个'void'方法。 I.E. 'public static void' –