gpt4 book ai didi

java - 尝试调用方法时出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:54 25 4
gpt4 key购买 nike

我正在尝试创建一个方法。根据我所阅读和观看的内容,虽然这是创建然后调用方法的方法,但我不太确定我是否做得正确。当我遵守时,我收到了错误,所以如果您对如何改进代码有任何建议,我们将不胜感激。

这是错误:

Error: can not find symbol.

它给了我大约 10 个错误,全部与 public static void getLabelData() 中的数组有关,我在其中尝试创建一个方法。

这是代码:

import javax.swing.JOptionPane;
public class MailOrderCall
{
public static void main(String[] args)
{
streetAddress, city, state, zip.
String nameAddressArray[] = new String[7];
String numBoxesInput;
int numBoxes;
String enterAnother = "Y";
int counter;



getLabelData();

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;
} // end while

// ask the user if finished entering mail orders
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");

// validate input for enterAnother... keep them here until they enter Y, y, N, 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"))
{
// if the user said they have more, then send prompts to read the next mail order input from user
getLabelData();
} // end if
} // end while
// successfully terminate the application
system.exit(0);
} // end main()

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): ");

numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order:");
numBoxes = Integer.parseInt(numBoxesInput);
}


} // end class

我对此还很陌生,所以如果我的代码完全是一场灾难,我很抱歉,我只是想弄清楚为什么它会给我一个错误。

最佳答案

您似乎正在尝试访问 getLabelData 函数中不可用的变量。如果你想在多个函数中使用一个变量,你应该在整个类的范围内声明它,如下所示:

public class MailOrderCallEMH {

String nameAddressArray[] = new String[7];
String numBoxesInput;

public static void main(String[] args) {
// ...
}

}

此外,streetAddress, city, state, zip. 看起来不像有效的 java 语法。您可能应该正常声明这些变量:

// Replace Object by the type you want
Object streetAddress;
Object city;
Object state;
Object zip;

希望这有帮助!

关于java - 尝试调用方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33965487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com