满足循环退出条件时执行其他语句

时间:2019-09-15 22:17:19

标签: 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");
       }
     }

1 个答案:

答案 0 :(得分:0)

您没有设置“ q”。您需要一个:

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