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' –