排序名称并使用 JOptionPane 显示输出
Sorting names and using JOptionPane to show output
到目前为止我的代码是这样的...
import javax.swing.*;
public class TestNo2{
public static void main(String[] args){
String[] nameNum = new String[0];
int numberNames;
JTextField inputName = new JTextField(5);
JPanel nameWindow = new JPanel();
nameWindow.add(new JLabel("How many names would you like to sort?"));
nameWindow.add(inputName);
int numNames = JOptionPane.showConfirmDialog(null, nameWindow
,"Accept and sort list of students."
,JOptionPane.OK_CANCEL_OPTION);
if(numNames == JOptionPane.OK_OPTION){
String numNamesS = inputName.getText();
numberNames = Integer.parseInt(numNamesS);
nameNum = new String[numberNames];
for(int counterOne=0;counterOne<nameNum.length;counterOne++){
for(int counterTwo=1;counterTwo<=nameNum.length;counterTwo++){
nameNum[numberNames] = JOptionPane.showInputDialog(null
,"Enter the name of student "+counterTwo
,"Name Input"
,JOptionPane.QUESTION_MESSAGE);
}
}
}
}
}
构建时没有错误,但是运行程序时,只允许输入一个名称,然后出现错误。
这是显示的错误。
线程异常 "main" java.lang.ArrayIndexOutOfBoundsException: 3
在 TestNo2.main(TestNo2.java:23)
感谢您花时间阅读本文。
for(int counterTwo=1;counterTwo<=nameNum.length;counterTwo++){
应该是:
for(int counterTwo=1;counterTwo < nameNum.length;counterTwo++){
因为表达式counterTwo<=nameNum.length
导致数组超出其边界(记住数组的实际长度从零开始并且1小于您指定的实际长度.
所以如果你有一个数组定义如下:
someArray[] something = {1,2,3,4,5,6};
something.length
的值将是 6
。但实际长度是5
(算作:0,1,2,3,4,5)。
为什么你有两个循环来获取名称和添加到字符串数组?一个就够了...
for(int counterOne=0;counterOne<nameNum.length;counterOne++){
nameNum[counterOne] = JOptionPane.showInputDialog(null
,"Enter the name of student "+(counterOne+1)
,"Name Input"
,JOptionPane.QUESTION_MESSAGE);
}
然后使用您打算使用的任何方法对数组 nameNum
进行排序。
到目前为止我的代码是这样的...
import javax.swing.*;
public class TestNo2{
public static void main(String[] args){
String[] nameNum = new String[0];
int numberNames;
JTextField inputName = new JTextField(5);
JPanel nameWindow = new JPanel();
nameWindow.add(new JLabel("How many names would you like to sort?"));
nameWindow.add(inputName);
int numNames = JOptionPane.showConfirmDialog(null, nameWindow
,"Accept and sort list of students."
,JOptionPane.OK_CANCEL_OPTION);
if(numNames == JOptionPane.OK_OPTION){
String numNamesS = inputName.getText();
numberNames = Integer.parseInt(numNamesS);
nameNum = new String[numberNames];
for(int counterOne=0;counterOne<nameNum.length;counterOne++){
for(int counterTwo=1;counterTwo<=nameNum.length;counterTwo++){
nameNum[numberNames] = JOptionPane.showInputDialog(null
,"Enter the name of student "+counterTwo
,"Name Input"
,JOptionPane.QUESTION_MESSAGE);
}
}
}
}
}
构建时没有错误,但是运行程序时,只允许输入一个名称,然后出现错误。
这是显示的错误。
线程异常 "main" java.lang.ArrayIndexOutOfBoundsException: 3 在 TestNo2.main(TestNo2.java:23)
感谢您花时间阅读本文。
for(int counterTwo=1;counterTwo<=nameNum.length;counterTwo++){
应该是:
for(int counterTwo=1;counterTwo < nameNum.length;counterTwo++){
因为表达式counterTwo<=nameNum.length
导致数组超出其边界(记住数组的实际长度从零开始并且1小于您指定的实际长度.
所以如果你有一个数组定义如下:
someArray[] something = {1,2,3,4,5,6};
something.length
的值将是 6
。但实际长度是5
(算作:0,1,2,3,4,5)。
为什么你有两个循环来获取名称和添加到字符串数组?一个就够了...
for(int counterOne=0;counterOne<nameNum.length;counterOne++){
nameNum[counterOne] = JOptionPane.showInputDialog(null
,"Enter the name of student "+(counterOne+1)
,"Name Input"
,JOptionPane.QUESTION_MESSAGE);
}
然后使用您打算使用的任何方法对数组 nameNum
进行排序。