我的程序收集四個房間收集的瓶子數量。當用戶在任何時候輸入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]);
}
}
}
確保您的腸衣匹配。 '而(退出。ToLower()==「quit」)' – user1231231412 2012-01-17 17:21:13