好吧,我得到我的程序「完成」,直到我無法解決這個簡單的問題。製作選項>保留<使用布爾值?
我得到這個:
package hotel;
import java.util.Scanner;
/**
*
* @author Defalt
*/
public class Hotel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Prices prices = new Prices();
Scanner user_in = new Scanner(System.in);
String method;
boolean taken = false;
boolean taken2 = false;
boolean taken3 = false;
boolean again = true;
//***********Introduction to the hotel*************
System.out.println("Welcome to Hotel Vermond's Heaven!");
System.out.println("We are happy to see that you would like to stay with us.");
//***********Creating of a Loop*************
do {
System.out.println("Please, type which room you would like to book: ");
System.out.println("Single Bed, Double Bed, President Suit");
method = user_in.nextLine();
//***********Choices of Rooms and Question to book another one*************
if ("Single Bed".equals(method)) {
System.out.println(prices.describe1());
if ("y".equals(user_in.nextLine())){
if(taken=true){
System.out.println("We are sorry, this room is already booked. Please choose another one");
}else again=true;
} else again=true;
} else if ("Double Bed".equals(method)){
System.out.println(prices.describe2());
if ("y".equals(user_in.nextLine())){
taken2 = true;
again=true;
} else again=true;
} else if ("President Suit".equals(method)){
System.out.println(prices.describe3());
if ("y".equals(user_in.nextLine())){
taken3 = true;
again=true;
} else again=true;
} else {
System.out.println("Please choose one of the rooms above.");
}
System.out.println("Would you like to book another Room(y/n)\n");
//***********Outbreak if User declines another booking*************
//***********Otherwise redo the whole process*************
if ("y".equals(user_in.nextLine())){
again=true;
} else{
break;
}
}while(again);
System.out.println("Thank you and goodbye!");
}
}
和我的其他類是這樣的:
package hotel;
import java.util.Scanner;
/**
*
* @author Defalt
*/
public class Prices {
Scanner user_in = new Scanner(System.in);
int price1 = 300;
int price2 = 800;
int price3 = 2500;
//***********Information of the Room including Validation Question*************
public String describe1() {
return
"You Choose the Single Bed.\n\tThis room Contains 1 Bed, 1 Fridge a Bathroom but no View on the Ocean.\n\tThis room will cost CHF " + price1 + ".-.\n\tWould you like to book this room?(y/n)";
}
public String describe2() {
return
"You Choose the Double Bed.\n\tThis room Contains 1 Queen-size Bed, 1 Fridge a bathroom, an Icemaker but no View on the Ocean.\n\tThis room will cost CHF " + price2 + ".-.\n\tWould you like to book this room?(y/n)";
}
public String describe3() {
return
"You Choose the President Suit.\n\tThis room Contains 1 King-size Bed, 1 Fridge, XXL Bathroom, Private Entertainment-System, 65inch Flatscreen and a Balcony with View on the Ocean.\n\tThis room will cost CHF " + price3 + ".-.\n\tWould you like to book this room?(y/n)";
}
}
正如你可以看到我試圖使Single Bed
保留房間已被服用後。然而,即使在第一次預訂時也會顯示System.out.println("We are sorry, this room is already booked. Please choose another one");
。
爲什麼會發生這種情況,儘管我的taken
布爾值的初始值爲false? 還有什麼其他選擇可以讓我的想法發揮作用?
PS !:我想我第一次得到它,但它保持了正確的值,無論我關閉程序並重新啓動它。 (只是一個側面說明)
啊是的。必須工作我的格式我承認這一點。正是== ......這樣簡單的錯誤。感謝您指出了這一點! – 2015-01-26 21:37:47
發生在我們身上! – gla3dr 2015-01-26 21:40:44
真的,我只是糾正了你所說的,我甚至在'System.out.println'之後做了'taken = true;'但是當我再次選擇單人牀時它不能糾正它。就好像條件不符合,布爾沒有改變。有任何想法嗎? (「單牀」.equals(方法)){System.out.println(prices.describe1()); (「y」.equals(user_in.nextLine())){if}(taken == true){System.out.println {「對不起,這個房間已經預定。請選擇另一個「); taken = true; } else again = true;' – 2015-01-26 21:48:19