java将数组传递给方法和错误
所以我有一个java程序包,它应该用来制作标签。我想用一种方法,但我不确定哪里不对,但我很确定这与我的方法有关
我现在遇到的错误是
错误:找不到符号,它引用了7个数组
import javax.swing.JOptionPane;
public class MailOrderpractice {
public static void main(String[] args) {
// declare variables
String nameAddressArray[] = new String[7];
String numBoxesInput;
int numBoxes;
String enterAnother = "Y";
int counter;
getLabelData();
numBoxesInput = JOptionPane
.showInputDialog("Enter number of boxes in the order:");
numBoxes = Integer.parseInt(numBoxesInput);
// begin outer loop logic that determines when user is finished entering
// mail orders
while (enterAnother.equalsIgnoreCase("Y")) {
counter = 1;
// begin the inner loop to display a label and increment the counter
while (counter <= numBoxes) {
System.out.println(nameAddressArray[0] + " "
+ nameAddressArray[1] + " " + nameAddressArray[2]);
System.out.println(nameAddressArray[3]);
System.out.println(nameAddressArray[4] + ", "
+ nameAddressArray[5] + " " + nameAddressArray[6]);
System.out.println("Box " + counter + " of " + numBoxes);
System.out.println();
counter = counter + 1;
}
enterAnother = " "; // initialize the variable to something other
// than "Y" before sending the prompt
enterAnother = JOptionPane
.showInputDialog("Do you want to produce more labels? Y or N");
while (!enterAnother.equalsIgnoreCase("Y")
&& !enterAnother.equalsIgnoreCase("N")) {
enterAnother = JOptionPane.showInputDialog(null,
"Invalid Response. Please enter Y or N.",
"DATA ENTRY ERROR", JOptionPane.ERROR_MESSAGE);
} // end while
if (enterAnother.equalsIgnoreCase("Y")) {
getLabelData();
numBoxesInput = JOptionPane
.showInputDialog("Enter number of boxes in the order:");
numBoxes = Integer.parseInt(numBoxesInput);
} // end if
} // end while
System.exit(0);
}
public static void getLabelData() {
nameAddressArray[0] = JOptionPane
.showInputDialog("Enter title (Mr., Ms., Dr., etc.): ");
nameAddressArray[1] = JOptionPane.showInputDialog("Enter first name: ");
nameAddressArray[2] = JOptionPane.showInputDialog("Enter lastname: ");
nameAddressArray[3] = JOptionPane
.showInputDialog("Enter street address: ");
nameAddressArray[4] = JOptionPane.showInputDialog("Enter city: ");
nameAddressArray[5] = JOptionPane
.showInputDialog("Enter state (IL, MO, etc.): ");
nameAddressArray[6] = JOptionPane
.showInputDialog("Enter zip (e.g., 62025): ");
}
}
# 1 楼答案
nameAddressArray在本地声明给main方法。可以将其设置为类级变量,也可以将其作为参数传递给getLabelData
# 2 楼答案
将}中,可能看起来
getLabelData
更改为声明、实例化和return
数组。然后将引用保存在main
中。在{而
getLabelData
可能看起来您还可以使用循环建立提示。差不多