-4

我需要编写一个程序,让用户输入导师姓名列表。最多只能聘请 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

4 回答 4

4

编辑:让我们也看看main方法:

public static void main(String[] args) {
    String[] listNames = getTutorNames();
    int[] numTutors = tutorNames();
}
  • 您没有使用您在getTutorNames. 如果您不打算将它们传递给其他任何东西,为什么要问它们?
  • tutorNames方法有一个int参数,但你没有传递任何参数
  • tutorNames方法被声明为返回 a String[],但您试图将结果分配给一个int[]变量。

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


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

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 对名称......但每次都将它们存储在firstNameand变量中。lastName你的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: ");
}
于 2013-04-05T19:32:14.540 回答
0

I'm just throwing this out there, because I can't make sense of the int[] numTutors = tutorNames() line, but try the following

public static void main(String[] args) {


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

assuming that String[] listNames = getTutorNames();, executed correctly, It should correctly sort your names. and then display them.

于 2013-04-05T19:59:26.230 回答
0

好吧,一方面看起来你的utorNames 函数的返回类型是一个字符串数组,但是你试图在main() 中将它分配给一个int 数组变量。

于 2013-04-05T19:34:42.130 回答
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]
        }
    }
}
于 2013-04-05T20:05:03.783 回答