更有效的方法来制作数字记忆程序

More efficient way to make a number memorisation program

本文关键字:数字 记忆 程序 有效 方法      更新时间:2023-10-16

我正在开发一个打印随机德语单词的程序,然后用户输入数字,如果正确,它会添加一个得分,如果不是,它什么也不做。我仍然是一个初学者,所以它只是很多 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"等。非常像英语中的"三十六"和"二十六",只是数字的顺序略有不同。

这意味着您可以通过使用三个随机数(每个在 09 之间(轻松解决问题,并将它们分别用作三个数组的索引。也许是这样的:

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为零,则特殊情况以及其他极端情况也可能有用。但希望你能得到这一切的一般要点。