将Array转换为主要方法以表示相同的结果,不使用仅LINQ int。

我的程序记录了四个房间在瓶子里收集的瓶子数量。 当用户输入退出时,显示每个房间收集的瓶子数量,并打印出具有最多瓶子的房间。 我用一个数组来跟踪房间号码。 如何更改方法而不是使用数组,我想启动room1,room2,room3,room4。 如果我不使用数组,我是否可以使用我的循环数组调用来写行? 我的意思是。

int room = int.Parse(quit); Console.Write("Bottles collected in room {0}: ", room); // This line adds the count of bottles and records it so you can continuously count the bottles collected. rooms[room - 1] += int.Parse(Console.ReadLine()); 

而这一行:

  }//Writes the bottles collected by the different rooms Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]); 

这是我的代码:

  namespace BottleDrive { class Program { static void Main(string[] args) {//Initialize array of rooms to 4 int[] rooms = new int[4]; //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; //Variable room holds the number of bottles collect by each room. int room = int.Parse(quit); Console.Write("Bottles collected in room {0}: ", room); // This line adds the count of bottles and records it so you can continuously count the bottles collected. rooms[room - 1] += int.Parse(Console.ReadLine()); } int maxValue = 0;//initiates the winner, contructor starts at 0 int maxRoomNumber = 0;//initiates the room number that wins for (int i = 0; i  maxValue)//Makes sure that the maxValue is picked in the array {//Looking for room number for the maxValue = rooms[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 + "!!!"); } } } 

谢谢你,我感谢这个社区帮助我学习了多少。

您无法使用单个变量轻松替换数组。 如果你有一个类似的声明

 int room1 = 0, room2 = 0, room3 = 0, room4 = 0; 

并且想要访问房间号i然后你必须写

 switch (i) { case 1: Console.WriteLine(room1); break; case 2: Console.WriteLine(room2); break; case 3: Console.WriteLine(room3); break; case 4: Console.WriteLine(room4); break; } 

使用数组,您可以简单地写

 Console.WriteLine(rooms[i]); 

如果你真的想采用这种无数组的方式,我建议你使用辅助方法:

 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) { switch (room) { case 1: return room1; case 2: return room2; case 3: return room3; case 4: return room4; default: return 0; } } 

您必须将变量room1到room4声明为类成员才能使其工作。

现在你可以写:

 Console.WriteLine(GetRoom(i)); 

或者代替rooms[i] += n;

 SetRoom(i, GetRoom(i) + n); 

你可以做到这一点,但这将是从良好的编程和编写错误代码的一步。

首先它会膨胀你的代码,因为你总是要做这样的事情:

 // this is the code you have now (3 lines for 4 rooms): int room = int.Parse(quit); Console.Write("Bottles collected in room {0}: ", room); rooms[room - 1] += int.Parse(Console.ReadLine()); // and this is the code you'll have when you refrain from using arrays // (17 lines for 2 rooms, ignoring empty lines and ones with only brackets): int room = int.Parse(quit); Console.Write("Bottles collected in room {0}: ", room); switch(room) { case 1: Console.WriteLine(room1); break; case 2: Console.WriteLine(room2); break; // other cases... } int count = int.Parse(Console.ReadLine()); switch(room) { case 1: room1 += count; break; case 2: room2 += count; break; // other cases... } 

第二个(更重要的是):

代码完全不可扩展。 如果你想添加第五个房间来收集瓶子,你必须完成整个程序并调整每个开关语句,这不仅耗时而且极易出错。

这是一个使用Class来保存每个房间信息的示例。 使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必跟踪另一个数组,您只需向该类添加属性即可。

各个房间现在被保存在列表而不是数组中,只是为了显示不同的结构。

这是新的Room类:

 public class Room { public int Number { get; set; } public int BottleCount { get; set; } public Room(int wNumber) { Number = wNumber; } } 

这是该程序的新版本。 请注意,添加了对最终用户输入的值的附加检查,以防止在尝试获取当前空间或将用户输入的值解析为int时出现exception:

  static void Main(string[] args) { const int MAX_ROOMS = 4; var cRooms = new System.Collections.Generic.List(); 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 + "!!!"); } 

我认为你的逻辑是合理的(即使用变量表示最大值和最大空间并比较每次)。 不使用数组有任何限制吗? 似乎使用4个变量需要更多的代码行。

我喜欢使用课堂的答案。 但是你可以用带有数组的标准c做到这一点。

有什么要求? 封装? 稳健性?