2015-01-26 24 views
0

好吧,我得到我的程序「完成」,直到我無法解決這個簡單的問題。製作選項>保留<使用布爾值?

我得到這個:

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 !:我想我第一次得到它,但它保持了正確的值,無論我關閉程序並重新啓動它。 (只是一個側面說明)

回答

1

您需要更改

if(taken=true){ 
      System.out.println("We are sorry, this room is already booked. Please choose another one"); 

if(taken==true){ 
      System.out.println("We are sorry, this room is already booked. Please choose another one"); 

注意雙等號的,如果條件。這將檢查taken的值,而不是總是將其設置爲true,這正是目前發生的情況。

另外,由於taken已經是一個布爾值,可以將其簡化爲這樣:

if(taken){ 
      System.out.println("We are sorry, this room is already booked. Please choose another one"); 

這可能會幫助您捕捉這些問題,如果你固定的格式和縮進。我傾向於避免隨時忽略大括號。這通常是Java標準格式:

if(condition) { 
    if(condition) { 
     // do the thing 
    } 
    else { 
     // do the other thing 
    } 
} 
else if(condition) { 
    // do another thing 
} 
else { 
    // do the third thing 
} 

注意如何IFS /其他IFS /別人的同一嵌套級別也保持在相同的縮進層次。這對保持直線有很大的幫助。

編輯:

我想指出的另一件事是,你有again布爾,但你不使用它。如果用戶完成,則不要使用break循環,而應使用again布爾值,並將其設置爲false。這會導致循環條件失敗並很好地退出循環。使用休息是醜陋的。

+0

啊是的。必須工作我的格式我承認這一點。正是== ......這樣簡單的錯誤。感謝您指出了這一點! – 2015-01-26 21:37:47

+0

發生在我們身上! – gla3dr 2015-01-26 21:40:44

+0

真的,我只是糾正了你所說的,我甚至在'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

相關問題