java - 这些Java错误是什么意思?

标签 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\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);
}

}

最佳答案

C:\Users\me\Documents\MailOrderEMH.java:27: error: variable numBoxes is already defined in method main(String[])
int numBoxes = Integer.parseInt(numBoxesString);



您已经两次声明了numBoxes
int numBoxes;
//...
int numBoxes = Integer.parseInt(numBoxesString);

C:\Users\me\Documents\MailOrderEMH.java:70: error: bad operand types for binary operator '||'
while (enterAnother == "Y" || "y")



条件需要解析为truefalse,其中"y"String,这没有任何意义。另外==并不是您如何在Java中比较String,更简单的解决方案是做类似...
 while ("Y".equalsIgnoreCase(enterAnother) {

C:\Users\me\Documents\MailOrderEMH.java:102: error: incompatible types: String cannot be converted to int
( "Enter Number of Boxes: " );


JOptionPane#showInputDialog返回String值,因此尝试将String分配给int将不起作用。

你可以做类似...
String value = JOptionPane.showInputDialog("Enter Number of Boxes: ");
if (value != null) {
    numBoxes = Integer.parseInt(value);
}

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

关于java - 这些Java错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662456/

相关文章:

java - Neo4J 结果未更新

C++ 移动语义编译错误 C2751

c - 错误: expected declaration specifiers or ‘...’ before ‘stdin’

javascript - "Uncaught SyntaxError: Cannot use import statement outside a module"尝试在 Django 应用程序的 js 文件中导入 vue.js

visual-studio - 如何在Visual C++ Express 2008/2010中启用错误突出显示?

java - 如何有条件地使用 "zipWhen"?

java - 使用Gson在Android中@SerializedName注解的基本目的是什么

c++ - 在 Code::Blocks on linux 中为 C++ GUI appz 设置 gtkmm-3.0

python - Tkinter-SyntaxError : non-keyword arg after keyword arg

java - 同步线程抛出 CurrentModificationException