我的程序記錄了4個房間收集到的瓶子數量。我是C#的初學者,但過去做過java。瓶子計數程序,開關語句查詢,查找最大數量
我不會使用LINQ,我不會使用數組。只有開關語句(對不起,我知道它效率低下)
我的程序必須記錄用戶輸入瓶子的數量,當用戶鍵入「退出」時,程序將假設吐出所有房間的瓶子數量,確定最具瓶子的房間作爲贏家。
我被困在這個switch語句中,我無法找到一種方法來啓動我的房間(room1,room2,room3,room4),它表示變量room1-4未分配。我應該能夠通過使用開關不斷地將瓶子添加到房間。
當我輸入quit時,程序可以吐出房間收集的所有瓶子,並找到瓶子最多的房間。
感謝您的時間,我非常感謝這個社區對我的幫助。
namespace BottleDrive1
{
class Program
{
static void Main(string[] args)
{//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
//Start of while loop to ask what room your adding into.
while (true)
{
Console.Write("Enter the room you're in: ");
//If user enters quit at anytime, the code will jump out of while statement and enter for loop below
string quit = Console.ReadLine();
if (quit == "quit")
//Break statement allows quit to jump out of loop
break;}}
private void SetRoom(int room, int value)
{
switch (room)
{
case 1:
room1 = value;
break;
case 2:
room2 = value;
break;
case 3:
room3 = value;
break;
case 4:
room4 = value;
break;
}
}
public int GetRoom(int room)
{
int count = int.Parse(Console.ReadLine());
switch (room)
{
case 1:
room1 += count;
break;
case 2:
room2 += count;
case 3:
room3 += count;
break;
case 4:
room4 += count;
break;
}
}
}
//This for statement lists the 4 rooms and their bottle count when the user has entered quit. An alternative to below
/*for (int i = 0; i < rooms.Length; ++i)
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);*/
/* int maxValue = 0;//initiates the winner, contructor starts at 0
int maxRoomNumber = 0;//initiates the room number that wins
for (int i = 0; i < room[i].Length; ++i)//This loop goes through the array of rooms (4)
{
if (room[i] > maxValue)//Makes sure that the maxValue is picked in the array
{//Looking for room number for the
maxValue = room[i];
maxRoomNumber = i + 1;
}//Writes the bottles collected by the different rooms
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);
}
//Outputs winner
Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
*/
}
該程序的最後一部分是我試圖找到最大值,因爲我用一個數組來啓動。我不能使用數組。
你需要移動room1,room2,room3和room4變量,以便它們是班級的一部分。你有他們在main()函數內。 – 2012-01-14 23:51:27