2015-11-29 47 views
0

所以我有一個Java程序設計製作標籤。我想用一種方法,但我不確定有什麼問題,但我很確定它與我的方法有關。將數組傳遞給方法和錯誤

我curently得到錯誤
錯誤:無法找到符號,它是在參考了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): "); 

    } 
} 

回答

0

更改getLabelData來聲明,instanitate和return數組。然後將參考文件保存在main中。在main可能的樣子,

String nameAddressArray[] = getLabelData(); 

getLabelData可能看起來像

public static String[] getLabelData() { 
    String[] nameAddressArray = new String[7]; 
    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): "); 
    return nameAddressArray; 
} 

你也可以建立你的提示用一個循環。像

static String[] prompts = { "title (Mr., Ms., Dr., etc.)", "first name", "lastname", // 
     "street address", "city", "state (IL, MO, etc.)", "zip (e.g., 62025)" }; 
public static String[] getLabelData() { 
    String[] nameAddressArray = new String[7]; 
    for (int i = 0; i < nameAddressArray.length; i++) { 
     nameAddressArray[i] = String.format("Enter %s: ", prompts[i]); 
    } 
    return nameAddressArray; 
} 
+0

謝謝你,我想我做到了這一點,它幾乎工作。現在唯一的問題是在輸出前詢問7陣列問題兩次。希望我能弄明白,謝謝 – Grace

0

nameAddressArray本地聲明的主要方法。您可以將其設置爲類級變量,或將其作爲參數傳遞給getLabelData。