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