输入扑克牌的序列号(从0到35),并确定扑克牌的花色和尊严

Enter the sequence number of the playing card (from 0 to 35) and determine the suit and dignity of the card

本文关键字:扑克牌 尊严 序列号 输入      更新时间:2023-10-16

我只能写36个开关,如何缩短代码?不能使用循环或其他函数,只能当和切换。(不要写 36 大小写或 36 如果,你可以少做(。

假设卡片按特定顺序表示,如下所示:

input   0 1 2 3 4 5 ... 9 10 11 ... 33 34 35 
suite   0 0 0 0 0 0 ... 1 1  1  ... 3  3  3
dignity 0 1 2 3 4 5 ... 0 1  2  ... 6  7  8

其中尊严 0 表示 6 可能,尊严 6、7、8 表示女王、国王和屁股(我们只是从零开始索引以保持简单(

然后你可以使用

int suite = input / 9;
int dignity = input % 9;

然后,您可以使用开关打印出正确的值(如果需要(:

std::cout << "dignity is ";
switch(dignity)
{
case 0: std::cout << "6"; break; //the dignity of the first card 
case 1: std::cout << "7"; break;
// ...
case 6: std::cout << "Queen"; break;
case 7: std::cout << "King"; break;
case 8: std::cout << "Ass"; break;
}
std::cout << "suite is ";
switch(suite)
{
case 0: std::cout << "...."; break; // name of the first suite.
// ...
}

让套件从 0 到 3,尊严从 0 到 8。

int x{0};
do
{
cout << "Enter a number from 0 to 35: ";
cin >> x;
} while (x < 0 || x > 35);
int suite{x / 9}, diginity{0}, subtract{0};
for (int i{suite}; i > 0; --i)
{
subtract += 9;
}
diginity = x - subtract;
cout << "Suit: " << suite << endl;
cout << "Dignity: " << diginity << endl;

x 越大,减去的金额就越大,得到一个介于 0 和 8 之间的数字。