用布尔值做一个选项 >reserved<?
making an option >reserved< with boolean?
好吧,我的程序 "finished" 到了无法解决这个简单问题的地步。
我明白了:
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!");
}
}
我的另一个 class 是这样的:
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
布尔值的初始值为假,但为什么会这样?
还有什么其他选择可以让我的想法付诸实践?
PS!:我想我第一次就让它工作了,但无论我关闭程序并重新启动它的频率如何,它都将获取的值保持为真。 (只是旁注)
你需要改变
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");
注意 if 条件中的双等号。这将检查 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/else ifs/else 如何也保持相同的缩进级别。这对保持笔直很有帮助。
编辑:
我想指出的另一件事是您有 again
布尔值,但您没有使用它。如果用户完成,不要 break
退出循环,而是使用你应该使用的 again
布尔值并将其设置为 false。这将导致循环条件失败并很好地退出循环。使用 break 是丑陋的。
好吧,我的程序 "finished" 到了无法解决这个简单问题的地步。
我明白了:
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!");
}
}
我的另一个 class 是这样的:
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
布尔值的初始值为假,但为什么会这样?
还有什么其他选择可以让我的想法付诸实践?
PS!:我想我第一次就让它工作了,但无论我关闭程序并重新启动它的频率如何,它都将获取的值保持为真。 (只是旁注)
你需要改变
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");
注意 if 条件中的双等号。这将检查 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/else ifs/else 如何也保持相同的缩进级别。这对保持笔直很有帮助。
编辑:
我想指出的另一件事是您有 again
布尔值,但您没有使用它。如果用户完成,不要 break
退出循环,而是使用你应该使用的 again
布尔值并将其设置为 false。这将导致循环条件失败并很好地退出循环。使用 break 是丑陋的。