2014-12-03 32 views
-4

我不能算出這個作業幫助:爲什麼不變量返回

我的變量沒有返回在我不知道我要去的地方錯誤的輸出信息打印和我的老師告訴我逐行檢查代碼並檢查邏輯和語法錯誤。任何幫助,將不勝感激。

package selfhelpstore; 
import javax.swing.JOptionPane; 
public class SelfHelpStore { 
    private static String getStringInput(String prompt) { 
     String input = JOptionPane.showInputDialog(prompt); 
     String nullFlag = "n"; 
     int i = 1; 
     while (i<=2) 
     if (input == null) { 
      System.exit(0); 
     }else if (input.isEmpty()) { 
      i++; 
      nullFlag = "y"; 
      prompt = JOptionPane.showInputDialog("Re-enter: "); 
     } else { 
      i=4; 
      nullFlag = "n"; 
     } 
     if (!input.isEmpty()) { 
      nullFlag = "n"; 
     } 
     if (nullFlag.equals("y")) { 
      JOptionPane.showMessageDialog(null, "Null or blank - exiting..."); 
      System.exit(0); 
     } 
     return prompt; 
    } 
    public static void main(String[] args) { 
     String openingMsg, nameInputMsg, customerName, nameOutputMsg, 
       returnInputMsg, customerReturn, returnOutputMsg, 
       greetingOutputMsg, outputMsg,coverInputMsg, coverMsg ; 

     openingMsg = "*** Welcome to Self Help Book Store Online Ordering System ***\n" 
        + "      It's a great day to read a book!"; 
     JOptionPane.showMessageDialog(null, openingMsg); 

     nameInputMsg = getStringInput("Please enter your name: "); 
       customerName = nameInputMsg; 
       returnInputMsg = getStringInput("Are you a returning customer (yes or no)?  "); 
     customerReturn = returnInputMsg; 
       coverInputMsg = getStringInput("would you like this book in hard cover or  paper back?"); 
       coverMsg = coverInputMsg; 

     nameOutputMsg  = "Welcome " + customerName + ".\n\n"; 
     returnOutputMsg = "Your return customer status is " + customerReturn + ".\n"; 
     greetingOutputMsg = "Thank you for visiting our Self Help Book Store!" + "\n" 
         + "Your " + coverMsg + " should be mailed in less than two  business days.\n"; 

     outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg; 
     JOptionPane.showMessageDialog(null, outputMsg); 
     System.exit(0); 
    } 
} 
+2

首先請正確縮進或我將能夠幫助 – realUser404 2014-12-03 21:32:03

+0

同上第一個評論,這使得它很難理解到底是怎麼回事之前死亡。並刪除所有無用的評論和空白。 – TooTiredToDrink 2014-12-03 21:33:17

+0

對不起,但StackOverflows很樂意回答特定的編程問題,或協助代碼中的特定路障,但我們不會通過它來給你答案。正如@ realUser404所說,一個好的第一步就是謹慎地縮進,但是你必須像你的老師所說的那樣去做,並逐行瀏覽它,直到找到不工作的部分。這是最有效的學習方式。 =) – HDCerberus 2014-12-03 21:33:52

回答

1

當您希望它返回輸入時,您的getStringInput方法正在返回變量提示。

private static String getStringInput(String prompt) { 
    String input = JOptionPane.showInputDialog(prompt); 

    String nullFlag = "n"; 
    int i = 1; 
    while(i <= 2) 

     if(input == null) 

     { 
      System.exit(0); 
     } else if(input.isEmpty()) 

     { 
      i++; 
      nullFlag = "y"; 
      prompt = JOptionPane.showInputDialog("Re-enter: "); 
     } else { 
      i = 4; 
      nullFlag = "n"; 
     } 

    if(!input.isEmpty()) { 
     nullFlag = "n"; 
    } 

    if(nullFlag.equals("y")) { 
     JOptionPane.showMessageDialog(null, "Null or blank - exiting..."); 
     System.exit(0); 
    } 
    return input; // Change this line 
} 
+0

@PatrickBallinger然後您將答案標記爲「接受」 – realUser404 2014-12-03 22:46:40