[英]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;
}
嘗試執行此操作的方法是將最小數量的數字放在某個地方,然后計算機將一組數字拉出並打印出來,然后用戶輸入數字,如果數字正確,則分數會上升。
例如
- 輸出:hundertsechsundzwanzig
輸入:126
正確
與其他語言不同,我知道在德語中如何將數字放在一起有些邏輯。 例如“ sechs unddreiß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
為零的特殊情況以及其他特殊情況也可能有用。 但希望您能掌握所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.