0

我一直在试图弄清楚如何为数组赋值。

stringName 数组的大小由用户输入(getNumberofStrings 方法)确定。我希望用户通过 JOptionPane 为 stringName 数组输入值。我写了一个方法(getStringName),但不知道如何从主方法中调用它。非常感谢对这个初学者的任何建议。

public class NoClassp3 {

public static void main(String[] args) {
    int numberOfStrings = getNumberOfStrings();
    String[] stringName = new String[numberOfStrings];
}   // end method main

public static int getNumberOfStrings() {
   String input = JOptionPane.showInputDialog("Enter number of strings in integer format");
   int number = Integer.parseInt(input);
   return number;
}   // end method getNumberOfStrings

public static String getStringName(String[] name) {
    String input = JOptionPane.showInputDialog("Enter string name");
    for (int i = 0; i < name.length; i++)
        name[i] = input;
    return input;
}   // end method getStringName
}   // end class NoClassp3
4

1 回答 1

0

尝试这个

public class NoClassp3 {

public static void main(String[] args) {
    int numberOfStrings = getNumberOfStrings();
    String[] stringName = new String[numberOfStrings];
    stringName = getStringName(stringName);

    for (int i = 0; i < stringName.length; i++)
    {
        System.out.println(stringName[i]);
    }
}   // end method main

public static int getNumberOfStrings() {
   String input = JOptionPane.showInputDialog("Enter number of strings in integer format");
   int number = Integer.parseInt(input);
   return number;
}   // end method getNumberOfStrings

public static String[] getStringName(String[] name) {
    for (int i = 0; i < name.length; i++)
        name[i] = JOptionPane.showInputDialog("Enter string name");
    return name;
}   // end method getStringName
}   // end class NoClassp3
于 2013-07-07T07:48:52.443 回答