0

プログラムの最後に尋ねられる質問「続行しますか? (y/n):」に対して、プログラムのユーザーが y または n 以外の値を入力したかどうかをキャッチする if ステートメントを作成しようとしています。しかし、「y」、「n」、または無効なものを入力しても、コンソールから「無効な入力を再試行してください」というメッセージが表示されます。これは、選択が y または n ではない場合にのみ発生するはずです。入力内容に関係なく、なぜそれが発生し続けるのか誰にもわかりませんか?

import java.util.Scanner;
public class ProductApp 
{
  public static void main(String[] args) 
  {
     //display a welcome message
      System.out.println("Welcome to the Product Selector ");
      System.out.println();

      // perform 1 or more selections
      Scanner sc = new Scanner(System.in);
      String choice = "y";
      while (choice.equalsIgnoreCase("y"))
      {
         System.out.println("Enter Product Code: ");
         String productCode = sc.next(); //read the product code
         sc.nextLine() ; //discard any other data entered on the line
         //make sure the case the user enters for a product code doesn't matter
         productCode = productCode.toLowerCase();
         // get the Product object
         Product p = ProductDB.getProduct(productCode) ;

         // display the output
         System.out.println();
         if (p != null)
             System.out.println(p);
         else
             System.out.println("No product matches this product code. \n");
         System.out.println("Product count: " + Product.getCount() + "\n" );

         // see if the user wants to continue
         System.out.println("Continue? (y/n): ");
         choice = sc.nextLine() ;
         System.out.println();
         if( !choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n") );
         {
             System.out.println("Invalid input try again");
             continue;
         }

      }
  }
}

また、「無効な入力を再試行してください」というメッセージが表示されるたびに、プログラムは新しい入力を一度要求しますが、それが有効かどうかに進みます。「y」の場合は再度実行され、それ以外の場合は有効な入力を再度要求する代わりに終了します。

4

7 に答える 7

2

あなたが持っているのであなたの状態は機能していません; ifステートメントの後。

if( !choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n") );
                                                                    ^^

変化する

if( !choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n") );
     {
         System.out.println("Invalid input try again");
         continue;
     }

if(!(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")))
     {
         System.out.println("Invalid input try again");
         continue;
     }
于 2013-03-15T06:20:33.897 に答える
2

あなたの状態は正しくありません

if(!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")){
    // choice is not y or n
}
于 2013-03-15T06:16:25.430 に答える
1

y または n を入力するまで確認する場合は、while ステートメントにします。それ以外の場合、大きな while ループにジャンプして終了します。これは、大きな while ループが要求しているためです。

while (choice.equalsIgnoreCase("y"))

したがって、内側のループは次のようになります。

while( !choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n") )
{
   System.out.println("Invalid input try again");
   //continue; //not needed
}

編集:別のアプローチは、「y」のみを「yes」として扱い、他のすべてを「n」として扱うことです

// pseudocode
while (!choice.equalsIgnoreCase("n")) {
   // do your thing
   if (!(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n"))) {
      choice = "n"; // so exit
   }
}
于 2013-03-15T06:16:47.587 に答える
1

choice.equalsIgnoreCase("y") && choice.equalsIgnoreCase("n") それは決して真実ではない

選択肢は y または n のいずれかであり、両方ではないため

于 2013-03-15T06:13:45.343 に答える
0
if( !(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")) )
     {
         System.out.println("Invalid input try again");
         continue;
     }

これを試して 。

于 2013-03-15T06:20:55.320 に答える
0

あなたの状態を確認してください。そのはず:

if(!(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n") ))
        {
        System.out.println("Invalid input try again");
        continue;
        }
于 2013-03-15T06:16:43.440 に答える
0

if(!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"));に変更する必要があると思いますif(!choice.equalsIgnoreCase("y") || !choice.equalsIgnoreCase("n")){&&両方の条件のチェックを使用すると true になりますchoiceが、値を 1 つしか含めることができないため、これが発生することはありません。

于 2013-03-15T06:15:41.787 に答える