2012-01-17 24 views
0

我的程序收集四個房間收集的瓶子數量。當用戶在任何時候輸入quit時,該程序將不再顯示,並顯示瓶子收集的結果。然後計算瓶數最多的獲勝室。如何在我的while循環中提示之前寫入線?

我有一個while循環我不明白爲什麼我不能輸入它。要進入while循環,它會提示我輸入數組中使用的數字1-4,以及每個房間收集到的瓶子數量。如果我隨時鍵入退出,程序將停止記錄瓶子並吐出結果以及瓶子最多的獲勝室。

如何獲得「輸入您所在的房間號」以便在輸入瓶子數之前先顯示出來?我的問題存在於getBottles()

我認爲這行不能在數組中使用,我是對嗎?

rooms [room - 1] + = int.Parse(Console.ReadLine());

namespace BottleDrive 
{ 
    class BottleDrive 
    { 
     public int[] rooms; 
     public BottleDrive() 
     { 
      rooms = new int[4]; 
     } 

     static void Main(string[] args) //static is member of class not object 
     { 
      BottleDrive bD = new BottleDrive(); 
      bD.getBottles(); 
      bD.displayBottleCount(); 
      bD.findWinner(); 
     } 
     public void getBottles() 
     { 
      string quit = Console.ReadLine(); 
      while while(quit != "quit") 
      { 
       int room = int.Parse(quit); 

       Console.Write("Bottles collected in room {0}: ", room); 
       rooms[room - 1] += int.Parse(Console.ReadLine()); 

       Console.Write("Enter the room you're in: "); 
      } 
     } 
     public void findWinner() 
     { 
      int maxValue = 0;//initiates the winner, contructor starts at 0 
      int maxRoomNumber = 0;//initiates the room number that wins 
      for (int i = 0; i < rooms.Length; ++i)//This loop goes through the array of rooms (4) 
      { 
       if (rooms[i] > maxValue)//Makes sure that the maxValue is picked in the array 
       {//Looking for room number for the 
        maxValue = rooms[i]; 
        maxRoomNumber = i + 1; 
       } 
      } 
      Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!"); 
     } 
     public void displayBottleCount() 
     { 
      Console.WriteLine("Bottles collected in room one: " + rooms[0]); 
      Console.WriteLine("Bottles collected in room two: " + rooms[1]); 
      Console.WriteLine("Bottles collected in room three: " + rooms[2]); 
      Console.WriteLine("Bottles collected in room four: " + rooms[3]); 
     } 
    } 
} 
+1

確保您的腸衣匹配。 '而(退出。ToLower()==「quit」)' – user1231231412 2012-01-17 17:21:13

回答

3

這條線:

while (quit == "quit") 

實際上應該是:

while (quit != "quit") 

或者更好的是:

while (!quit.Equals("quit", StringComparison.CurrentCultureIgnoreCase)) 

這將忽略大小寫的輸入。正如其他人所指出的那樣,循環中存在更多問題。嘗試使用此爲您getBottles功能:

public void getBottles() 
{ 
    string input; 

    do 
    { 
     Console.Write("Enter the room you're in: (or quit)"); 
     input = Console.ReadLine(); 

     int room; 
     // doing try parst because the input might be "quit" or other junk 
     if (int.TryParse(input, out room)) 
     { 
      Console.Write("Bottles collected in room {0}: ", room); 
      // this will fail hard if the input is not of type int 
      rooms[room - 1] += int.Parse(Console.ReadLine()); 
     } 
    } while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase)); 
} 
+0

謝謝,做聲明是要走的路,並在之後嵌套。極好! – gli 2012-01-17 17:44:35

1

你應該有while(quit != "quit")或者條件嗎?

4
while (quit == "quit") 

如果退出(你從控制檯得到的)是「退出」,上面的行只會運行循環。

你想:

while (quit != "quit") 

while (!quit.Equals("quit")) 

畢竟,雖然你還沒有真正更新您的循環中退出所以一旦你將永遠不會離開。

您需要捕獲您的console.Readline並將其放入「quit」。

你可能也想看看int.TryParse以防人們輸入的字符串不是退出或有效整數。 TryParse會告訴你解析是否成功,而不是引發異常。

0

嘗試:

while (!quit.Equals("quit")) 
0

功能getBottles()看起來很奇怪。當你輸入「quit」時,你只輸入while循環。我不認爲這是你想要的行爲。

+0

是的,我剛剛提到過,我希望它能夠提示我先輸入房間號碼,然後再輸入房間收集到的瓶子。 getBottles(),格式錯誤或代碼爲 rooms [room - 1] + = int.Parse(Console.ReadLine());錯誤? – gli 2012-01-17 17:29:40

相關問題