2
我似乎無法讓它在最後一節倒數。我試圖創建一個新的變量,減去1,並將該變量放在最後一節中,它適用於所有數字,但數字爲9,19,29,39 ... 99。有人可以給我一個提示或指向正確的方向嗎?我也試着創建另一個循環來倒數,並從數組中讀取它。我只有幾個月的時間才瞭解這一點,不知道還有什麼可以嘗試的。我想我曾經到了這個地步,至少對我來說,困難的一部分在我身後,並不是那麼重要。謝謝。它是Visual Studio 2010中的C++。在一個作業中,我得到了一個應該編寫的程序來輸出歌詞到牆上的99瓶啤酒,
#include <iostream>
#include <string>
using namespace std;
string tens[11] = {""," Ten"," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
string ones[10] = {"","-One","-Two","-Three","-Four","-Five","-Six","-Seven","-Eight","-Nine"};
string s_ones[11] = {" Zero"," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
string other[ ] = {" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen",""};
int main()
{
for(int i = 99; i >= 0; --i)
{
int tens_place = i/10, ones_place = i % 10, small_ones_place = i % 10;
if(10 <= i && i < 20)
{
cout << other[i - 10] << " bottles of beer on the wall. \n"
<< other[i - 10] << " bottles of beer.\n"
<< " Take one down, pass it around,\n"
<< other[(i - 10)] << " bottles of beer on the wall.\n\n";
}
if(20 <= i && i <= 99)
cout << tens[tens_place] + ones[ones_place] << " bottles of beer on the wall. \n"
<< tens[tens_place] + ones[ones_place] << " bottles of beer.\n"
<< " Take one down, pass it around,\n"
<< tens[tens_place] + ones[ones_place] << " bottles of beer on the wall.\n\n";
if(2 <= i && i < 10)
cout << s_ones[small_ones_place] << " bottles of beer on the wall.\n"
<< s_ones[small_ones_place] << " bottles of beer.\n"
<< " Take one down, pass it around,\n"
<< s_ones[small_ones_place] << " bottles of beer on the wall.\n\n";
if(i == 1)
cout << s_ones[small_ones_place] << " bottle of beer on the wall.\n"
<< s_ones[small_ones_place] << " bottle of beer.\n"
<< " Take one down, pass it around,\n"
<< s_ones[small_ones_place] << " bottles of beer on the wall.\n\n";
if(i == 0)
cout << s_ones[small_ones_place] << " bottles of beer on the wall.\n"
<< s_ones[small_ones_place] << " bottles of beer.\n"
<< " Go to the store and get some more,\n"
<< " Ninety-nine bottles of beer on the wall.\n\n";
}
return(0);
}
感謝您的快速反應,我試過了,它只適用於那個範圍內的數字,10到20個。我無法讓它適用於其他數字。當我爲int tens_place創建新變量時,ones_place和small_ones_place我也從那裏減去了一個,但它不能正確輸出9,19,29,39 ....等等。它只留下一個空白或上面的數字。 – Aaron
@ user1848538:當ones_place == 0時,您只需要條件邏輯。在這種情況下,您的新的tens_place減少1,並且您的新ones_place爲9. –
謝謝,我終於明白了。它可能比它需要的時間長,但它做它應該做的。我結合了s_ones和其他數組,添加了二十個數據,然後添加了一個像您所建議的那樣的條件邏輯語句,並且工作正常。感謝您的幫助。 – Aaron