2013-07-27 45 views
0

我似乎無法通過此錯誤。我的代碼:方法無法應用於給定類型/找不到符號

import java.util.*; 
public class Collector { 

    public static void Names() { 

     java.util.Scanner input = new java.util.Scanner(System.in); 

    // Prompt the user to enter the number of students 
    System.out.print("Enter the number of students: "); 
    int numberOfStudents = input.nextInt(); 

    // Create arrays 
    String[] names = new String[numberOfStudents]; 
    double[] scores = new double[numberOfStudents]; 

    // Enter student name and score 
    for (int i = 0; i < scores.length; i++) 
     { 
     System.out.print("Enter student's name: "); 
     names[i] = input.next(); 
     System.out.print("Enter student's exam score: "); 
     scores[i] = input.nextDouble(); 
     System.out.println(" "); 
      } 

    } 
     void SortRoutine (String[] names, double[] scores) { 
      for (int i = scores.length - 1; i >= 1; i--) 
    { 
     // Find the maximum in the scores[0..i] 
     double currentMax = scores[0]; 
     int currentMaxIndex = 0; 

     for (int j = 1; j <= i; j++) 
     { 
     if (currentMax < scores[j]) 
     { 
      currentMax = scores[j]; 
      currentMaxIndex = j; 
     } 
     } 

     //arrange values as necessary 
     if (currentMaxIndex != i) 
     { 
     scores[currentMaxIndex] = scores[i]; 
     scores[i] = currentMax; 
     String temp = names[currentMaxIndex]; 
     names[currentMaxIndex] = names[i]; 
     names[i] = temp; 


     } 
    } 

    // Print student data 
    System.out.println(" "); 
    System.out.println("***** Student Scores Sorted High to Low *****"); 
    System.out.println(" "); 
    for (int i = scores.length - 1; i >= 0; i--) 
    { 
     System.out.println(names[i] + "\t" + scores[i] + "\t"); 
    } 
     System.out.println(" "); 
    } 
    } 

主要方法:

import java.util.*; 
import java.util.Arrays; 
public class NameCollector { 

    public static void main(String[] args) { 
    Collector collect = new Collector(); 
    collect.Names(); 
    collect.SortRoutine(); 
    } 
} 

如果我從收集類的第28行刪除參數我得到cannot find symbol errors。我相信這意味着Jcreator無法找到數組值。我怎麼會讓我的第一個方法中定義的數組值在第二個方法中可見?如果我在第28行中留下參數,則錯誤消息是:

C:\Users\Dark Prince\Documents\JCreator LE\MyProjects\NameCollector\src\NameCollector.java:16: error: method SortRoutine in class Collector cannot be applied to given types; 
collect.SortRoutine(); 
    ^
    required: String[],double[] 
    found: no arguments 
    reason: actual and formal argument lists differ in length 
1 error 

過程完成。

我在想我不應該使用參數,並使它可以通過排序方法看到數組值,但我真的只是想讓事情工作。

+0

這兩個類所在的包是什麼? –

回答

0

你問:我會如何去使第一種方法中定義的數組值在第二種方法中可見?

有很多方法可以做到這一點。這是做它的一種方式(可能不是最好的):

你可以把數組爲靜態實例成員在收集類是這樣的:

public class Collector { 

    static String[] names; 
    static double[] scores; 

    public static void Names() { 

然後當你創建的陣列名稱的方法,你可以這樣做:

// Create arrays 
names = new String[numberOfStudents]; 
scores = new double[numberOfStudents]; 

最後你SortRoutine從改變方法的簽名:

void SortRoutine (String[] names, double[] scores) 

void SortRoutine() 
+0

啊我看到了,我試圖讓數組變成靜態的,但是我在方法中聲明瞭它們,而不是類。謝謝你的提示! –

+0

@MaxwellBell如果我的答案解決了您的問題,請考慮接受它。 – jpw

相關問題