2011-10-25 22 views
-3

此問題要求編寫一個接受五個'商店'輸入的程序。理想情況下,輸入範圍應爲100到2000.每個輸入應除以100,並以星號顯示該數量(即500是*等)。我相信我有第一部分,但我不知道如何去做其餘的事情。我不能使用數組,因爲我還沒有學到它們,我想自己學習,而不是從另一個學生複製粘貼。到目前爲止,我只有如何從循環中存儲多個值,並根據輸入顯示星號

int loop; 
loop = 1; 

while (loop <= 5) 
{ 
    string input1; 
    int iinput1, asteriskcount1; 
    Console.WriteLine("Input number of sales please!"); 
    input1 = Console.ReadLine(); 
    //store value? 

    loop = loop + 1; 
    input1 = Convert.ToInt32(input1); 
    asteriskcount1 = iinput1/10; 
} 
+0

兩個家庭作業的問題在一個小時?聽起來你應該做一些閱讀。 – Gabe

+0

在過去的5個小時裏,我實際上一直在努力掙扎其中的6個。我能夠使用這本書沒有問題的其他四個,但我只是無法弄清楚這兩個 – Josh

+5

我很高興這個網站不存在,當我正在學習計算機科學。編寫代碼的部分學習過程是反覆試驗。 – Gabe

回答

0

超級簡單

 
int asteriskCount = int.Parse(input1)/ 100; 
string output = new string('*', asteriskCount); 
1

不知道如果我理解你想要做什麼。但也許這會有所幫助。這是未經測試的,但它應該做我認爲你問的東西,但我不確定你想用星號做什麼。請解釋更多,如果這不是你所得到的。

string Stored = ""; 
    for (int i=0; i < 5; i++;) 
    { 
     string input1; 
     int iinput1, asteriskcount1; 
     Console.WriteLine("Input number of sales please!"); 
     input1 = Console.ReadLine(); 
     //Adds to existing Stored value 
     Stored += input1 + " is "; 

     //Adds asterisk 
     iinput1 = Convert.ToInt32(input1); 
     asteriskcount1 = iinput1/100; 
     for(int j = 0; j < asteriskcount1; j++) 
     { 
      Stored += "*"; 
     } 

     //Adds Comma 
     if(i != 4) 
      Stored += ","; 

    } 
    Console.WriteLine(Stored); //Print Result 
0

不想寫出來給你,但這裏的一些想法...

第一,可以爲5家商店for循環做:

for (int loop = 0; loop < 5; loop++) 

你」你可能會想要asterickCount(而不是asterickCount1),因爲你在循環中。你還需要除以100,因爲你的範圍可以達到2000,並且在控制檯上有80個字符。這意味着它將打印多達20個星號。

你會想PrintAstericks(int count);函數,你在計算你調用的asterickCount之後調用它。該函數只是簡單地調用Console.Write(而不是WriteLine)來編寫一個星號n次(新字符串有重載以獲取char和count)。

但是,該模式將在您輸入每個輸入後打印星號。如果您希望模式爲(1)接受五個商店的計數,然後(2)爲所有五個商店打印星號行,則需要一個包含5個插槽的數組來存儲輸入,然後遍歷數組並打印星號行。

最後,你會想對輸入進行一些驗證。看看Int32.TryParse:

http://msdn.microsoft.com/en-us/library/bb397679.aspx

相關問題