2012-01-14 61 views
0

我的程序記錄了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 + "!!!"); 
     */ 
    } 

該程序的最後一部分是我試圖找到最大值,因爲我用一個數組來啓動。我不能使用數組。

+0

你需要移動room1,room2,room3和room4變量,以便它們是班級的一部分。你有他們在main()函數內。 – 2012-01-14 23:51:27

回答

0

下面是一個使用一類以保持每個房間的信息的示例。使用類的原因是,如果您的程序將來需要更改以收集更多信息,則不必跟蹤另一個數組,您只需向該類添加屬性即可。

單個房間現在保存在列表中而不是數組中,只是爲了顯示不同的構造。

這是新的房類:

public class Room 
{ 
    public int Number { get; set; } 
    public int BottleCount { get; set; } 

    public Room(int wNumber) 
    { 
     Number = wNumber; 
    } 
} 

這裏是程序的新版本。注意:最終用戶輸入的值的額外檢查是爲了防止異常試圖讓當前室內或解析爲int由用戶輸入的值時被添加:

static void Main(string[] args) 
    { 
     const int MAX_ROOMS = 4; 
     var cRooms = new System.Collections.Generic.List<Room>(); 

     for (int nI = 0; nI < MAX_ROOMS; nI++) 
     { 
      // The room number is 1 to 4 
      cRooms.Add(new Room(nI + 1)); 
     } 

     // Initializes the room that wins 
     //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 roomNumber = Console.ReadLine(); 
      if (roomNumber == "quit") 
      { 
       //Break statement allows quit to jump out of loop 
       break; 
      } 
      int room = 0; 
      if (int.TryParse(roomNumber, out room) && (room < MAX_ROOMS) && (room >= 0)) { 
       Room currentRoom; 

       currentRoom = cRooms[room]; 

       Console.Write("Bottles collected in room {0}: ", currentRoom.Number); 

       int wBottleCount = 0; 

       if (int.TryParse(Console.ReadLine(), out wBottleCount) && (wBottleCount >= 0)) 
       { 
        // This line adds the count of bottles and records it so you can continuously count the bottles collected. 
        currentRoom.BottleCount += wBottleCount; 
       } 
       else 
       { 
        Console.WriteLine("Invalid bottle count; value must be greater than 0"); 
       } 
      } 
      else 
      { 
       Console.WriteLine("Invalid room number; value must be between 1 and " + MAX_ROOMS.ToString()); 
      } 
     } 

     Room maxRoom = null; 

     foreach (Room currentRoom in cRooms) //This loop goes through the array of rooms (4) 
     { 
      // This assumes that the bottle count can never be decreased in a room 
      if ((maxRoom == null) || (maxRoom.BottleCount < currentRoom.BottleCount)) 
      { 
       maxRoom = currentRoom; 
      } 
      Console.WriteLine("Bottles collected in room {0} = {1}", currentRoom.Number, currentRoom.BottleCount); 
     } 
     //Outputs winner 
     Console.WriteLine("And the Winner is room " + maxRoom.Number + "!!!"); 
    } 
1

只需推動INT聲明這樣Main方法以上:

class Program 
{ 
    int room1 = 0; 
    .... 
    static void Main(string[] args) 
    { 
    .... 
    } 
}