[英]Else statement executing when loop exit condition is met
因此,我有一個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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.