java - 满足循环退出条件时执行的else语句

标签 java

所以我有这个 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

关于java - 满足循环退出条件时执行的else语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57948586/

相关文章:

java - 在 fxml 中定义您的自定义类

java - Maven 发布插件 SSL 问题

java - andengine中如何应用渐变背景

java - 为什么将函数参数声明为 final?

java - 如何在 openGL 中制作光球?

java - Java中的多线程最佳实践

java - 如何判断我从网络接收的文本/从文件读取的文本是否使用给定的编码?

java - 我无法从服务器端套接字读取

java - 在 AWS 上部署 Java Web 应用程序 + MongoDB

java - 将按钮添加到 jtable