更有效的方法来制作数字记忆程序
More efficient way to make a number memorisation program
我正在开发一个打印随机德语单词的程序,然后用户输入数字,如果正确,它会添加一个得分,如果不是,它什么也不做。我仍然是一个初学者,所以它只是很多 if 语句,有没有办法让我只有一个具有最小数字的函数,然后计算机将它们随机放在一起,同时还能够区分正确答案和错误答案?
如何制作一个随机的德语单词生成器,而不必为每个数字做一个 if 语句?
~编辑 1
start:
string zero = "Null", one = "Eins", two = "Zwei", three = "Drei", four = "Vier", five = "Funf";
int ans, score = 0;
srand(time(0));
int x = rand() % 5;
if (x == 0)
{
cout << zero;
cin>>ans
if (ans == 0)
{
score = score + 1;
goto start;
}
else
goto start;
}
if (x == 1)
{
cout << one;
goto start;
}
我试图这样做的方法是将最少数量的数字放在某个地方,然后计算机将一组数字拉出并打印出来,然后用户输入数字,如果他们是正确的,他们的分数就会上升。
例如
-输出:百水
山-输入:126
正确
与其他语言不同,我知道德语中数字的组合方式有一些逻辑。例如"sechs und dreißig"、"sechs und zwanzig"等。非常像英语中的"三十六"和"二十六",只是数字的顺序略有不同。
这意味着您可以通过使用三个随机数(每个在 0
和 9
之间(轻松解决问题,并将它们分别用作三个数组的索引。也许是这样的:
std::string const hundreds[10] = {
"",
"hundert",
"zweihundert",
// etc...
};
std::string const tens[10] = {
"",
"", // Special case for the tens
"und swanzig",
"und dreißig",
// etc...
}
std::string const ones[10] = {
"null",
"ein",
"zwei",
// etc...
};
unsigned hundred_digit = get_random_number_between_0_and_9();
unsigned ten_digit = get_random_number_between_0_and_9();
unsigned one_digit = get_random_number_between_0_and_9();
std::string number = hundreds[hundred_digit] + ' ' + ones[one_digit] + ' ' + tens[ten_digit];
如上面的评论中所述,如果ten_digit
1
那么您需要对此进行一些特殊处理。不过仍然可以用数组解决它。如果one_digit
为零,则特殊情况以及其他极端情况也可能有用。但希望你能得到这一切的一般要点。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 松弛原子与无同步情况下的记忆连贯性
- 更有效的方法来制作数字记忆程序
- 面试难题:用有限的记忆对输入的一百万个数字进行排序
- 根据记忆打印奇怪的数字