【问题标题】:method cannot be applied to given types / cannot find symbol方法不能应用于给定类型/找不到符号
【发布时间】:2013-07-27 17:09:12
【问题描述】:

我似乎无法克服这个错误。我的代码:

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();
    }
}

如果我从 Collector 类的第 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

过程完成。

我在想我不应该使用参数并使其可以通过排序方法看到数组值,但实际上我只是想让该死的东西起作用。

【问题讨论】:

  • 这两个类驻留在哪些包中?

标签: java arrays class methods invocation


【解决方案1】:

您问:如何让第一个方法中定义的数组值对第二个方法可见?

有很多方法可以做到这一点。这是一种方法(可能不是最好的):

您可以像这样在 Collector 类中将数组转换为静态实例成员:

public class Collector {

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

    public static void Names () {

然后当你在 Names 方法中创建数组时,你会这样做:

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

最后,您将 SortRoutine 的方法签名更改为:

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

void SortRoutine ()

【讨论】:

  • 啊,我明白了,我曾尝试将数组设为静态,但我是在方法中声明它们,而不是在类中。谢谢你的提示!
  • @MaxwellBell 如果我的答案解决了您的问题,请考虑接受它。
猜你喜欢
  • 2016-06-20
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多