排序数组......似乎无法得到它

时间:2013-04-05 19:28:39

标签: java arrays sorting

我需要编写一个程序,让用户输入导师姓名列表。最多只能雇用10名同伴辅导员。然后,程序将根据按姓氏按字母顺序排列的列表显示每个名称。这就是我到目前为止所拥有的。我一直在弄清楚如何对数组进行数小时排序,我似乎无法弄明白。如果有人能用简单的语言向我解释我做错了什么,那么我不会再犯同样的错误,我会很感激。

以下是我遇到的错误:

 errors: PeerTutorReport.java:11: error: method tutorNames in class PeerTutorReport cannot be applied to       given types;
    int[] numTutors = tutorNames();
                      ^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
PeerTutorReport.java:46: error: cannot find symbol
    String[] listNames = new String[numTutor];
                                    ^
 symbol:   variable numTutor
 location: class PeerTutorReport
  PeerTutorReport.java:48: error: cannot find symbol
    for (x = 0; x <= listNames.length; x++) {
         ^
  symbol:   variable x
 location: class PeerTutorReport
  PeerTutorReport.java:48: error: cannot find symbol
    for (x = 0; x <= listNames.length; x++) {
                ^
 symbol:   variable x
 location: class PeerTutorReport
 PeerTutorReport.java:48: error: cannot find symbol
    for (x = 0; x <= listNames.length; x++) {
                                       ^
 symbol:   variable x
  location: class PeerTutorReport
 PeerTutorReport.java:49: error: cannot find symbol
    numTutors[x] = JOptionPane.showMessageDialog(" Tutor LAST NAME and FIRST NAME Listed in              Alphabetically Order"+(x+1)+ " " + "For example: 'Smith, John'");
              ^

这是我的代码。我的第一种方法没有问题,但我的第二种方法......我遇到了麻烦。

import javax.swing.JOptionPane;
import java.util.Arrays;


public class PeerTutorReport {
    public static void main(String[] args) {
        String[] listNames = getTutorNames();
        int[] numTutors = tutorNames();
    }

    public static String[] getTutorNames() {
        String firstName;
        String lastName;
        String[] listNames = new String[10];

        for (int x = 0; x < listNames.length; x++) {
            firstName = JOptionPane.showInputDialog(null, "Enter Tutor's First Name: ");
            lastName = JOptionPane.showInputDialog(null, "Enter Tutor's Last Name: ");
            listNames[x] = lastName + ", " + firstName;
        }
        return listNames;
    }

    public static String[] tutorNames(int numTutors) {
        String[] listNames = new String[numTutor];

        for (x = 0; x <= listNames.length; x++) {
            numTutors[x] = JOptionPane.showMessageDialog(
                    "Tutor LAST NAME and FIRST  NAME Listed in Alphabetically Order"
                            + (x + 1) + " " + "For example: 'Smith, John'");
            Arrays.sort(listNames);
        }
        return listNames;
    }
}

4 个答案:

答案 0 :(得分:4)

编辑:让我们看一下main方法:

public static void main(String[] args) {
    String[] listNames = getTutorNames();
    int[] numTutors = tutorNames();
}
  • 您不是使用您在getTutorNames中提取的名称。如果你不打算将它们传递给其他任何东西,为什么还要求它们呢?
  • tutorNames方法有int参数,但您没有传递任何参数
  • 声明tutorNames方法返回String[],但您尝试将结果分配给int[]变量。

请注意,这些问题的 none 与排序有关 - 它们比这更基本。我建议你停下来,仔细查看所有代码


编辑:好的,让我们看看另一种方法:

public static String[] tutorNames(int numTutors) {
    String[] listNames = new String[numTutor];

    for (x = 0; x <= listNames.length; x++) {
        numTutors[x] = JOptionPane.showMessageDialog("...");    
        Arrays.sort(listNames);
    }
    return listNames;
 }   

尽管numTutors[x]numTutors,您仍尝试分配给int。那不行。由于new String[numTutor]变量不存在,因此numTutor也不存在。

你也在每次迭代时对数组进行排序(从不填充)...为什么?


编辑:在问题得到纠正之前我写了这个答案......

嗯,这是第一个问题:

for (int x = 0; x < listNames.length; x++) {
    firstName = JOptionPane.showInputDialog(null, "Enter Tutor's First Name: ");
    lastName = JOptionPane.showInputDialog(null, "Enter Tutor's Last Name: ");
}

您要求10对名称......但每次只需将它们存储在firstNamelastName变量中。永远不会触及您的listNames变量。你可能想要这样的东西:

for (int x = 0; x < listNames.length; x++) {
    String firstName = JOptionPane.showInputDialog(null, "Enter Tutor's First Name: ");
    String lastName = JOptionPane.showInputDialog(null, "Enter Tutor's Last Name: ");
    listNames[x] = lastName + ", " + firstName;
}

请注意我是如何在循环中移动firstNamelastName的变量声明 - 无论如何你都没有在循环之外使用它们,因此提前声明它们没有任何好处。通常,您希望尽可能地限制局部变量的范围。

或者只是要求每位导师使用一个名字:

for (int x = 0; x < listNames.length; x++) {
    listNames[x] = JOptionPane.showInputDialog(null, "Enter Tutor's Name: ");
}

答案 1 :(得分:0)

嗯,有一件事看起来你的tutorNames函数上的返回类型是一个字符串数组,但你试图将它在main()中分配给一个int数组变量。

答案 2 :(得分:0)

我只是把它扔到那里,因为我无法理解int[] numTutors = tutorNames()行,但请尝试以下

public static void main(String[] args) {


    String[] listNames = getTutorNames();
    Arrays.sort(listNames);
    for(int i=0; i<listNames.length; i++)
    {
        JOptionPane.showMessageDialog(listNames[i]);
    }
}

假设String[] listNames = getTutorNames();正确执行,它应该正确排序你的名字。然后显示它们。

答案 3 :(得分:0)

我不打算为你做功课,但这是用伪代码形成你的程序的更好方法:

public class Whatever {

    public static void main(String[] args) {
        int desiredListSize = 10;
        String[] listNames = getTutorNames(intDesiredListSize);

        // if you display the list of names right now,
        // they should be in the order the user entered them
        displayListOfNamesToUser(listNames);

        // now sort the array
        Arrays.sort(listNames);

        // now display it again, it should be sorted
        displayListOfNamesToUser(listNames);
    }

    public static String[] getTutorNames(int numberOfInputs) {
        String firstName, lastName;
        String[] names = new String[numberOfInputs];
        for (int x = 0; x < numberOfInputs; x++) {
            firstName = JOptionPane.showInputDialog(null, "Enter Tutor's First Name: ");
            lastName = JOptionPane.showInputDialog(null, "Enter Tutor's Last Name: ");
            listNames[x] = lastName + ", " + firstName;
        }
        return names;
    }

    public void displayListOfNamesToUser(String[] names) {
        for (int x = 0; x < names.length; x++) {
            //display names[x]
        }
    }
}