我似乎無法通過此錯誤。我的代碼:方法無法應用於給定類型/找不到符號
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
過程完成。
我在想我不應該使用參數,並使它可以通過排序方法看到數組值,但我真的只是想讓事情工作。
這兩個類所在的包是什麼? –