2013-04-18 174 views
0

java方法statithing,但我似乎無法讓我的第56行的while語句正確調用我的方法。有什麼我做錯了嗎?我對Java非常陌生,所以不勝感激!提前致謝! 這裏是我的代碼:爲什麼我的程序不能調用我的方法?

import java.util.*; 
import javax.swing.*; 
import java.io.*; 

public class GradeCalculator { 
    static String fileInput; 
    static double totalGrade; 
    static int scoreCount= 0; 
    static double classAverage; 
    static double score; 
    static double testScore; 
    static double averageScore; 
    static int numberOfStudents = 0; 
    static char letterGrade; 
    static String fileOutput; 
    static String nameOfStudent; 
    static int numberCount; 
    static int numberCalculatedStudents; 
    static double average = 0; 

    public static void main (String[] args) throws FileNotFoundException { 
    //Welcome 

    JOptionPane.showMessageDialog(null,"Welcome to the Grade Calculator! This program will\n" + 
               " calculate the average percentage of 5 test scores that\n"+ 
               " are given in a given file once these scores are\n"+ 
               " averaged it will give the student a letter grade.","Welcome!",JOptionPane.PLAIN_MESSAGE); 
    fileInput = JOptionPane.showInputDialog(null, "Please enter the name of the input file you wish to use for this program." 
              ,"Input File",JOptionPane.PLAIN_MESSAGE); 
    fileOutput = JOptionPane.showInputDialog(null, "Please enter the name of the output file you wish to use for this program." 
              ,"Output File",JOptionPane.PLAIN_MESSAGE); 
    //preparing text files 
    PrintWriter outFile = new PrintWriter (fileOutput);           
    File inFile = new File (fileInput); 

    Scanner reader = new Scanner(new FileReader(fileInput)); 
    outFile.println("Student Name Test1 Test2 Test3 Test4 Test5 Average Grade"); 

    while(reader.hasNextLine()) { 
     nameOfStudent = reader.next(); 
     outFile.printf("%n%n%s",nameOfStudent); 
     numberOfStudents++; 
     score = reader.nextDouble(); 
     calculateAverage(score); 
     calculateGrade(averageScore); 
     outFile.printf("        %.2f ", averageScore); 
     outFile.println("                "+letterGrade); 
    } 
    classAverage = classAverage/numberCalculatedStudents;  
    outFile.print("\n\n\n\n\n\n\n\n\n\n\n\nClass average for "+ numberCalculatedStudents + "of" + numberOfStudents + "is" + classAverage); 
    JOptionPane.showMessageDialog(null,"The report has successfully been completed and written into the file of " + fileOutput +"." 
                +" The class average is " + classAverage + ". Please go to the output file for the complete report."); 
    outFile.close(); 
    } 

    public static void calculateAverage(double score) throws FileNotFoundException { 
     Scanner reader = new Scanner(new FileReader(fileInput)); 
     PrintWriter outFile = new PrintWriter (fileOutput); 
     while (reader.hasNextDouble() && numberCount <= 5) { 
      score = reader.nextDouble(); 
      numberCount++; 
     if (score >= 0 & score <= 100) { 
       outFile.printf("%10.2f",score); 
      scoreCount++; 
      average = score + average; 
     } 
     else 
      outFile.printf("    **%.2f",score); 
     } 
     if (average!=0){ 
      numberCalculatedStudents++; 
      average = average/scoreCount; 
      averageScore = average; 
      classAverage = average + classAverage; 
      } 

      average = 0; 
    } 

    public static char calculateGrade (double averageScore) { 

     if (averageScore >= 90 && averageScore <= 100) 
      letterGrade = 'A'; 
     else if (averageScore < 90 && averageScore >= 80) 
      letterGrade = 'B'; 
     else if (averageScore < 80 && averageScore >= 70) 
      letterGrade = 'C'; 
     else if (averageScore < 70 && averageScore >= 60) 
      letterGrade = 'D'; 
     else if (averageScore < 60 && averageScore >= 0) 
      letterGrade = 'F'; 
     else 
      letterGrade =' '; 

     return letterGrade; 
    } 
} 
+1

哪一行是56? –

+0

難道你不能告訴我們問題在哪裏,而不是引用行號? – maba

+0

56是哪裏? – drewmoore

回答

0

不知道哪一行的問題引起的,有兩個問題我跳出:

在while循環的頂部:

if (score >= 0 & score <= 100) 
    { outFile.printf("%10.2f",score); 
     scoreCount++; 
     average = score + average; 
    } 
    else 
     outFile.printf("    **%.2f",score);} 

你在else語句後有一個近括號(}),但沒有開括號。因此,左括號看起來像它在你想要的時候退出while循環。其次,它看起來像你正在試圖在你的CalculateGrade方法中返回一些東西(也就是一個char),但是你已經指定了返回類型的void,這意味着即使你有一個return語句,也不會有任何東西當你調用它時會返回。您不會顯示您調用該方法的位置,因此我不能確定這是否會造成問題,但它確實存在疑問。好像你要使用:的

public static char calculateAverage(double score) throws FileNotFoundException{ 

代替public static void calculateAverage(double score)...

而且,是有一個原因,所有這些方法是靜態的?你知道什麼是靜態的嗎?

EDIT(根據您的評論):

號使一個變量static使其成爲一個「類變量」,這意味着,只有其中一個存在該類的所有對象。爲了說明:

如果你有一個這樣的類:

class test { 
static int id; 
} 

並運行下面的代碼:

test t1 = new test(); 
    test t2 = new test(); 

    t1.id = 4; 
    t2.id = 8; 

    System.out.println(t1.id); 

將打印8.這是因爲,由於ID是一個static在類的任何對象上改變它的變量都會導致它爲類的每個其他對象改變。

這與「實例變量」相反,其中每個對象都有一個「實例變量」。要使id成爲實例變量,只需刪除static關鍵字。如果你這樣做並運行相同的代碼,它將打印4,因爲改變t2的實例變量對t1沒有影響。

有意義嗎?

相關問題