初学者c++避免重复if/else if语句
Beginner C++ avoiding repetitive if/else if statements
我正在做一个座位表程序。该程序的目的是显示带有座位价格的座位表,并允许用户选择特定的座位或基于价格的座位。除了一些例外,我基本上已经使它正常工作了。
我在这里发布了一个关于其中一个例外的问题,但很快被告知,首先我需要解决每个价格的重复代码的问题。我需要找出一个单独的代码块,将实现这对每个价格范围。我已经看过了,但不太确定从哪里开始。有什么建议吗?
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS = 9;
const int COLUMNS = 10;
int main()
{
bool isDone = false;
string rowNumber[] =
{
"Row 1: ",
"Row 2: ",
"Row 3: ",
"Row 4: ",
"Row 5: ",
"Row 6: ",
"Row 7: ",
"Row 8: ",
"Row 9: ",
};
int seatingChart[ROWS][COLUMNS] =
{
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{20, 20, 30, 30, 40, 40, 30, 30, 20, 20},
{20, 30, 30, 40, 50, 50, 40, 30, 30, 20},
{30, 40, 50, 50, 50, 50, 50, 50, 40, 30},
};
// Prints array to the screen
cout << "t Please choose a seat or a price: nn" << endl;
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
cout << " --------------------------------------" << endl;
for (int row = 0; row < 9; row++)
{
cout << rowNumber[row];
for (int column = 0; column < 10; column++)
{
cout << seatingChart[row][column] << " ";
}
cout << endl;
}
cout << "n" << endl;
// Main Program Loop
do
{
char input;
cout << "Press (S) to select a specific seatn";
cout << "Press (P) to select a seat based on pricen";
cout << "Press (Q) to quitnn";
cout << "Your selection: ";
cin >> input;
// Select a specific seat by it's coordinates
if (input == 's' || input == 'S')
{
int xCoord;
int yCoord;
cout << "nPlease input the row number: ";
cin >> yCoord;
int seatRow = yCoord - 1;
cout << "Please input the seat number: ";
cin >> xCoord;
int seatNumber = xCoord - 1;
if (seatingChart[seatRow][seatNumber] == 0)
{
cout << "nI'm sorry that seat has been sold. Please select a different seat." << endl;
}else
{
cout << "nThe seat you selected is $" << seatingChart[seatRow][seatNumber] << endl;
seatingChart[seatRow][seatNumber] = 0;
}
// Select a seat based off of price
}else if (input == 'p' || input == 'P')
{
int seatPrice;
cout << "Please enter a seat price: $";
cin >> seatPrice;
// $10 seats
if (seatPrice == 10)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 10 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "n" << endl;
}
}
}
}
}
// $20 seats
else if (seatPrice == 20)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 20 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "n" << endl;
}
}
}
}
}
// $30 seats
else if (seatPrice == 30)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 30 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "n" << endl;
}
}
}
}
}
// $40 seats
else if (seatPrice == 40)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 40 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "n" << endl;
}
}
}
}
}
// $50 seats
else if (seatPrice == 50)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 50 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "n" << endl;
}
}
}
}
}else // Input validation
{
cin.fail();
cout << "nSorry, there are no seats available for that price" << endl;
}
}else if (input == 'q' || input == 'Q')
{
isDone = true;
}else
{
cin.fail();
cout << "nInvalid selection" << endl;
}
cout << "n" << endl;
}while (isDone == false);
return 0;
}
你已经知道你需要的一切了。
检查座位价格是否有效,使用||
:
if (seatPrice == 10 || seatPrice == 20 || seatPrice == 30)
{
find a seat
}
else
{
tell user that price is invalid
}
检查座位价格是否等于用户输入的价格,使用seatingChart[row][column] == seatPrice
(而不是固定的数字10、20、30、40或50)。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 具有嵌套 if-else 的循环的时间复杂度
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 简化对两个布尔值的 4 个 if/else 检查
- 如何在C++中创建 if else 循环而不是多个嵌套?
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- 如何解释 #if/#else 位域?(VC++菜鸟)
- if-else 语句仅按特定顺序工作,我不知道为什么