我知道我的代码仍然存在缺陷(正在进行中)。但是我遇到了这两个错误,我不明白为什么。
任何帮助都非常感谢,谢谢!!
这些是错误
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")
条件需要解析为
true
或false
,其中"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/