2017-09-16 10 views
-1

私は最後にプログラムを終了するプロンプトで簡単な "ストア"プログラムを作ろうとしています。ユーザーが「はい」と入力すると、終了します。しかし、彼らがいいえと言うならば、ショッピングを続けることができることを知らせるメッセージを印刷するだけです。何らかの理由でメッセージが表示されません。ここに目的があります。このプログラムの最後の出力行がNetBeansに表示されないのはなぜですか?

public class HelloShopper1 { 
    public static void main(String[] args) { 
       Scanner scanner = new Scanner(System.in); 
       System.out.print("Hello Shopper, what is your name?\n"); 
       String input = scanner.nextLine(); 
       if(!input.matches("[a-zA-Z_]+")) { 
       System.out.println("Nice try " + input + " . Get out of my store!"); 
       } else { 
      System.out.println("Thank you, " + input + ". It is my pleasure to help you today."); 
      System.out.println("Do you want to close this program?"); 
      String input1 = scanner.nextLine(); 
      System.out.println(input1); 
      if(input1 == "yes") { 
      System.exit(0); 
      if(input1 == "no") { 
      System.out.println("Thank god. Please continue shopping."); 
      } 
     } 
    } 
} 
+1

文字列を比較するために、 '=='使用しないでください。 '.equals(...)'や '.equalsIgnoreCase(...)'メソッドを使います。あなたがテストに本当に関心を持っているのと同じ方法で、* function * equalityのために、 '=='が* reference *の等価性をチェックすることを理解してください。 –

答えて

0
import java.util.*; 
public class HelloShopper1 
{ 
public static void main(String[] args) 
{ 
      Scanner scanner = new Scanner(System.in); 
      System.out.print("Hello Shopper, what is your name?\n"); 
      String input = scanner.nextLine(); 
      if(!input.matches("[a-zA-Z_]+")) 
      { 
      System.out.println("Nice try " + input + " . Get out of my 
     store!"); 
      }else{ 
     System.out.println("Thank you, " + input + ". It is my pleasure to 
     help you today."); 
     System.out.println("Do you want to close this program?"); 
     String input1 = scanner.nextLine(); 
     if(input1.equalsIgnoreCase("yes")) 
     { 
     System.exit(0); 
     } 
     if(input1.equalsIgnoreCase("no")) 
     { 
     System.out.println("Thank god. Please continue shopping."); 
     } 
     } 
     } 
     } 
関連する問題