Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这些Java错误意味着什么?_Java_Compiler Errors_Syntax Error - Fatal编程技术网

这些Java错误意味着什么?

这些Java错误意味着什么?,java,compiler-errors,syntax-error,Java,Compiler Errors,Syntax Error,我知道我的代码仍然有缺陷(这是一项正在进行的工作)。但我犯了这两个错误,我不明白为什么。 非常感谢您的帮助,谢谢 这些都是错误 C:\Users\me\Documents\MailOrderEMH.java:27: error: variable numBoxes is already defined in method main(String[]) int numBoxes = Integer.parseInt(numBoxesString); ^ C:\Users\m

我知道我的代码仍然有缺陷(这是一项正在进行的工作)。但我犯了这两个错误,我不明白为什么。 非常感谢您的帮助,谢谢

这些都是错误

C:\Users\me\Documents\MailOrderEMH.java:27: error: variable numBoxes is already defined in method main(String[])
    int numBoxes = Integer.parseInt(numBoxesString);
        ^
C:\Users\me\Documents\MailOrderEMH.java:70: error: bad operand types for binary operator '||'
     while (enterAnother == "Y" || "y")
                                ^
  first type:  boolean
  second type: String
C:\Users\me\Documents\MailOrderEMH.java:102: error: incompatible types: String cannot be converted to int
            ( "Enter Number of Boxes: " );
            ^
3 errors

Tool completed with exit code 1
这是代码

import javax.swing.JOptionPane; // Imports JOptionPane class.

public class MailOrderEMH
{
   public static void main( String[] args )
   {
    // Declare string variables
    String title;
String firstName;
String lastName;
String streetAddress;
String city;
String state;
String zip;
String numBoxesString;
int numBoxes;
int count = 1;
String enterAnother = "Y"; //INITILIZE the loop control variable

//get input values from user
numBoxesString = JOptionPane.showInputDialog
( "Enter Number of Boxes: " );

//Conver srring to integer
int numBoxes = Integer.parseInt(numBoxesString);

//get input values from user
 title = JOptionPane.showInputDialog
( "What is your title ex. (Ms. Mr. Dr.) " );

//get input values from user
firstName = JOptionPane.showInputDialog
( "Enter First Name: " );

//get input values from user
lastName = JOptionPane.showInputDialog
( "Enter Last Name: " );

//get input values from user
streetAddress = JOptionPane.showInputDialog
( "Enter Street Address: " );

//get input values from user
city = JOptionPane.showInputDialog
( "Enter City: " );

//get input values from user
state = JOptionPane.showInputDialog
( "Enter State: " );

//get input values from user
zip = JOptionPane.showInputDialog
( "Enter Zip Code: " );


while (count <= numBoxes)
{
    System.out.println( title + firstName + lastName );
    System.out.println( streetAddress );
    System.out.println( city + state + zip );
    System.out.println( "Box" + count + "of" + numBoxes);
    count = count + 1;
}
//get input values from user
enterAnother = JOptionPane.showInputDialog
( " Do you want to produce more labels? Y or N " );

 while (enterAnother == "Y" || "y")
{
        //get input values from user
         title = JOptionPane.showInputDialog
        ( "What is your title ex. (Ms. Mr. Dr.) " );

        //get input values from user
        firstName = JOptionPane.showInputDialog
        ( "Enter First Name: " );

        //get input values from user
        lastName = JOptionPane.showInputDialog
        ( "Enter Last Name: " );

        //get input values from user
        streetAddress = JOptionPane.showInputDialog
        ( "Enter Street Address: " );

        //get input values from user
        city = JOptionPane.showInputDialog
        ( "Enter City: " );

        //get input values from user
        state = JOptionPane.showInputDialog
        ( "Enter State: " );

        //get input values from user
        zip = JOptionPane.showInputDialog
        ( "Enter Zip Code: " );

        //get input values from user
        numBoxes = JOptionPane.showInputDialog
        ( "Enter Number of Boxes: " );
}
// End program.
         System.exit(0);
}
import javax.swing.JOptionPane;//导入JOptionPane类。
公共类MailOrderEMH
{
公共静态void main(字符串[]args)
{
//声明字符串变量
字符串标题;
字符串名;
字符串lastName;
字符串地址;
字符串城市;
字符串状态;
拉链;
字符串numboxessstring;
内特牛;
整数计数=1;
String enterother=“Y”//初始化循环控制变量
//从用户获取输入值
numBoxesString=JOptionPane.showInputDialog
(“输入框数:”);
//转换为整数
int numBoxes=Integer.parseInt(numboxessString);
//从用户获取输入值
title=JOptionPane.showInputDialog
(“你的头衔是什么,例如(博士先生女士”);
//从用户获取输入值
firstName=JOptionPane.showInputDialog
(“输入名字:”);
//从用户获取输入值
lastName=JOptionPane.showInputDialog
(“输入姓氏:”);
//从用户获取输入值
streetAddress=JOptionPane.showInputDialog
(“输入街道地址:”);
//从用户获取输入值
city=JOptionPane.showInputDialog
(“输入城市:”);
//从用户获取输入值
state=JOptionPane.showInputDialog
(“进入状态:”);
//从用户获取输入值
zip=JOptionPane.showInputDialog
(“输入邮政编码:”);
而
C:\Users\me\Documents\MailOrderEMH.java:27:错误:变量numBoxes
已在方法main(字符串[])中定义
int numBoxes=Integer.parseInt(numboxessString)

您已经声明了两次
numBoxes

int numBoxes;
//...
int numBoxes = Integer.parseInt(numBoxesString);
C:\Users\me\Documents\MailOrderEMH.java:70:错误:操作数类型错误 对于二进制运算符“| |”
while(输入另一个==“Y”| |“Y”)

条件需要解析为
true
false
,其中
“y”
是一个
字符串
,这毫无意义。而且
=
不是Java中比较
字符串
的方式,更简单的解决方案是执行以下操作

 while ("Y".equalsIgnoreCase(enterAnother) {
String value = JOptionPane.showInputDialog("Enter Number of Boxes: ");
if (value != null) {
    numBoxes = Integer.parseInt(value);
}
C:\Users\me\Documents\MailOrderEMH.java:102:错误:不兼容的类型:无法将字符串转换为int
(“输入框数:”)

JOptionPane#showInputDialog
返回一个
字符串
值,因此尝试将
字符串
分配给
int
将不起作用

你可以做一些像

 while ("Y".equalsIgnoreCase(enterAnother) {
String value = JOptionPane.showInputDialog("Enter Number of Boxes: ");
if (value != null) {
    numBoxes = Integer.parseInt(value);
}

请记住,
JOptionPane.showInputDialog
如果用户取消该对话框,也可以返回
null
,因此您需要为最终结果做好准备

谢谢您的帮助,但这是指哪个错误。第二个错误?我尝试将其更改为while(输入另一个.equal(“Y”|“Y”))它仍然给了我完全相同的错误建议
enterather==“y”| | enterather==“y”
“y”好得多。equalsIgnoreCase(enterather)
,看到OP是一个初学者,并且误解了类C语言中
|
与“or”的区别在英语中,@Rhymoid Except
==
不是Java中比较字符串的方式!我的错误。不过,我还是想让步骤更明确一点,用
“y”。equals(enterather)| y。equals(enterather)
(中间)建议。@MadProgrammer非常感谢我按照你说的做了,它修复了除了我仍然得到的numBoxes之外的所有错误:不兼容的类型:String不能转换为int(“输入框数:”)我照你说的做了,把字符串value=JOptionPane.showInputDialog(“输入框数:”);if(value!=null){numBoxes=Integer.parseInt(value);}但我仍然得到了错误我发现我的代码中的第二个错误是98。因为这是一个while循环,如果答案是“Y”,我怎么能为numboxes获得另一个输入?如果答案是“Y”,我会因为两次衰减sring值而对我大喊大叫,为什么第二个错误是ony