2015-11-12 172 views
-2

我知道我的代碼仍然存在缺陷(它正在進行中)。但是,我得到了這些夫婦的錯誤,我不明白爲什麼。 任何幫助都表示讚賞謝謝!這些Java錯誤意味着什麼?

論文是錯誤

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); 
} 

}

+1

[我如何準備在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer

+0

感謝您的幫助,但哪個錯誤是在這裏refinance。第二個? – Grace

+0

我試着改變它while(enterAnother.equal(「Y」||「y」)),它仍然給我完全相同的錯誤 – Grace

回答

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,這是沒有意義的。此外==不在於你如何比較String S IN Java中,一個簡單的解決辦法是做這樣的事情......

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如果對話框被用戶取消,所以您需要爲eventuallaity

+0

建議'enterAnother ==「y」|| enterAnother ==「Y」'比「Y」.equalsIgnoreCase(enterAnother)'好得多,因爲OP是初學者,誤解了C語言中的'||'與英語中的「or」不同。 –

+0

@Rhymoid除了== ==不是'String'比較在Java中的工作原理 – MadProgrammer

+0

對!我的錯。不過,我會用''y「.equals(enterAnother)||使步驟更加明確「Y」.equals(enterAnother)'作爲(中級)建議。 –

相關問題