嵌套if else问题 [英] Nested if else question
问题描述
样品输出
输入房间类型:1
输入月份:Dec
输入没有天数:3
输入房间类型:2
输入月份:Jan
输入日期:4
输入房间类型:0
要支付的总金额:.......
那里只有3种类型的房间。 1,2和3.如果用户输入任何其他如果应出现无效的房间类型。问题是可以通过为房间类型输入0来退出,如示例输出中所示。但在我的情况下,它表示无效的房间类型和后显示总金额。你可以帮我吗。
我尝试过:
#include
sample output
enter type of the room: 1
enter the month: Dec
enter no of days: 3
enter type of the room: 2
enter the month: Jan
enter no of days: 4
enter type of the room: 0
total amount to be paid: .......
There are only 3 type of rooms. 1, 2 and 3. if user enters any other if should appear "Invalid room type". The question says can exit by entering 0 for the room type as shown in the sample output. but in my case it says invalid room type and after shows total amount. can you please help me.
What I have tried:
#include
推荐答案
while(true)
{
// Get your value
...
switch(enteredRoomType)
{
case 0:
// All done - exit the app.
exit(0);
case 1:
case 2:
case 3:
// All ok.
break;
default:
cout << "Invalid room type";
continue;
}
...
}
不应该嵌套如果
。那应该是如果
chain (一系列if-else if
语句)或者,更好的切换
构造。
That shouldn't be nestedif
. That should beif
chain (a sequence ofif-else if
statements) or, better aswitch
construct.
这篇关于嵌套if else问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!