Java 满足循环退出条件时执行的Else语句

Java 满足循环退出条件时执行的Else语句,java,Java,所以我有这个while循环,我想通过用户输入q或q退出循环,如果用户输入任何其他字母,我希望程序输出“请再试一次” 现在,当用户输入q时,我的程序确实会停止,但它也会打印“请再试一次”。我意识到这是因为当用户输入“q”时,userChoice并不等于那些选择之外的内容,但我不确定如何更改它,因此当程序退出时,它会退出而不说“请再试一次” 我只是再次运行程序,并意识到当我输入q或q时,循环实际上并没有结束 public void processTransactions() {

所以我有这个while循环,我想通过用户输入q或q退出循环,如果用户输入任何其他字母,我希望程序输出“请再试一次”

现在,当用户输入q时,我的程序确实会停止,但它也会打印“请再试一次”。我意识到这是因为当用户输入“q”时,userChoice并不等于那些选择之外的内容,但我不确定如何更改它,因此当程序退出时,它会退出而不说“请再试一次”

我只是再次运行程序,并意识到当我输入q或q时,循环实际上并没有结束

public void processTransactions()
    {
        Scanner sc = new Scanner(System.in).useDelimiter("\n");
        userChoice = "x";
         while(!userChoice.equals("q") || (!userChoice.equals("Q")))
        {
         System.out.println("Welcome to the Amazon listings.");
         System.out.println(" ");
         System.out.println("Enter \"A\" to add item");
         System.out.println("Enter \"F\" to find an Item");
         System.out.println("Enter \"D\" to delete an Item");
         System.out.println("Enter \"S\" to show all Items in the catalog");
         System.out.println("Enter \"Q\" to quit");
         userChoice = sc.next();
         if((userChoice.equals("A") ) || (userChoice.equals("a"))){
                System.out.println("What type of Item do you want to add?");
                type = sc.next();
                System.out.println("Please enter the Item number");
                itemNum = sc.nextInt();
                //title does not work with spaces for some reason
                System.out.println("Please enter the title");
                title = sc.next();
                System.out.println("Please enter the price");
                price = sc.nextDouble();
             if((type.equals("Book")) || (type.equals("book"))){
                 System.out.println("Please enter the authors name");
                 author = sc.next();
                 Book newItem = new Book(this.itemNum, this.title, this.price, this.author);
                 this.cat.addItem(newItem);
                 System.out.println("New book added to catalog");
                 System.out.println(" ");
                }
             if((type.equals("Movie")) || (type.equals("movie"))){
                 System.out.println("Please enter the name of the movie star");
                 star = sc.next();
                 Movie newItem = new Movie(this.itemNum, this.title, this.price, this.star);
                 this.cat.addItem(newItem);
                 System.out.println("New Movie added to catalog");
                 System.out.println(" ");
                }
             if((type.equals("Music")) || (type.equals("music"))){
                 System.out.println("Please enter the name of the artist");
                 artist = sc.next();
                 Music newItem = new Music(this.itemNum, this.title, this.price, this.artist);
                 this.cat.addItem(newItem);
                 System.out.println("New music added to catalog");
                 System.out.println(" ");
                }
             if((userChoice.equals("Q")) || (userChoice.equals("q"))){
               break;
             }
        }
        else if((userChoice.equals("S")) || (userChoice.equals("s"))){
            this.cat.printList();
        }
        else if((userChoice.equals("F")) || (userChoice.equals("f"))){
            System.out.println("Enter the item number of the item you wish to find");
            itemNum = sc.nextInt();
            Item item = this.cat.find(itemNum);
            if(item != null){
                System.out.println(item.toString());
            }
            else{
                System.out.println("not found");
            }
        }
        else if ((userChoice.equals("D")) || (userChoice.equals("d"))){
            System.out.println("Enter the item number of the item you wish to delete");
            itemNum = sc.nextInt();
            Item item = this.cat.find(itemNum);
            if(item != null){
                this.cat.remItem(item);
                System.out.println("Item Removed");

            }
            else{
                System.out.println("not found");
            }
        }


        else 
       {
            System.out.println("please try again");
       }
     }
您没有设置“q”。您需要一个:

else if(userChoice.equals("q") || userChoice.equals("Q")) { break; } // Leaves while loop
您没有设置“q”。您需要一个:

else if(userChoice.equals("q") || userChoice.equals("Q")) { break; } // Leaves while loop

您可以使用
!userChoice.equalsIgnoreCase(“q”)
以避免
!userChoice.equals(“q”)| |(!userChoice.equals(“q”)
您可以使用
!userChoice.equalsIgnoreCase(“q”)
来避免
!userChoice.equals(“q”)|(!userChoice.equals(“q”)