C# 有人能将此数组代码转换为非数组方法以获得相同的结果吗?
我这里有一段代码,记录了4个房间收集的瓶子数量。当用户输入quit时,程序会显示每个房间收集的瓶子数量,并确定收集瓶子数量最多的房间。我使用了一个数组方法,但我不应该使用这个方法,只是为了说明数组是多么有用。有人能给我指点吗C# 有人能将此数组代码转换为非数组方法以获得相同的结果吗?,c#,arrays,methods,C#,Arrays,Methods,我这里有一段代码,记录了4个房间收集的瓶子数量。当用户输入quit时,程序会显示每个房间收集的瓶子数量,并确定收集瓶子数量最多的房间。我使用了一个数组方法,但我不应该使用这个方法,只是为了说明数组是多么有用。有人能给我指点吗 namespace BottleDrive { class Program { static void Main(string[] args) {//Initialize array of rooms to 4
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());
}
//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 < 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;
}//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 + "!!!");
}
}
}
namespace驱动器
{
班级计划
{
静态void Main(字符串[]参数)
{//将房间数组初始化为4
int[]房间=新int[4];
//开始while循环,询问添加到哪个房间。
while(true)
{
控制台。写下(“进入你所在的房间:”;
//如果用户随时输入quit,代码将跳出while语句并在下面输入for循环
string quit=Console.ReadLine();
如果(退出=“退出”)
//Break语句允许quit跳出循环
打破
//可变房间保存每个房间收集的瓶子数量。
int room=int.Parse(退出);
控制台。写入(“在{0}房间收集的瓶子:”,房间);
//此行添加瓶子数并记录,以便您可以连续计数收集的瓶子。
rooms[room-1]+=int.Parse(Console.ReadLine());
}
//此for语句列出了用户输入quit时的4个房间及其瓶数。下面是另一个选项
/*对于(int i=0;imaxValue)//确保在数组中拾取maxValue
{//正在查找房间号以便
maxValue=房间[i];
maxRoomNumber=i+1;
}//写不同房间收集的瓶子
WriteLine(“在{0}={1}房间收集的瓶子”,i+1,房间[i]);
}
//输出优胜者
WriteLine(“获胜者是房间”+maxRoomNumber+“!!!”);
}
}
}
如果不允许使用数组,则可以为所有数组项声明4个变量
int room1, room2, room3, room4;
我想这是本练习的预期方法。如果不允许使用数组,可以为所有数组项声明4个变量
int room1, room2, room3, room4;
我想这就是本练习的预期方法。下面是一个使用类保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必再跟踪另一个数组,只需向类添加属性即可 各个房间现在保存在一个列表中,而不是一个数组中,只是为了显示不同的构造 这是新的房间等级:
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 + "!!!");
}
static void Main(字符串[]args)
{
const int MAX_房间=4间;
var cRooms=new System.Collections.Generic.List();
对于(int-nI=0;nI=0)){
房间当前房间;
currentRoom=cRooms[房间];
Console.Write(“在{0}房间收集的瓶子:”,currentRoom.Number);
int wBottleCount=0;
if(int.TryParse(Console.ReadLine(),out wBottleCount)&&(wBottleCount>=0))
{
//此行添加瓶子数并记录,以便您可以连续计数收集的瓶子。
currentRoom.BottleCount+=wBottleCount;
}
其他的
{
Console.WriteLine(“无效瓶数;值必须大于0”);
}
}
其他的
{
Console.WriteLine(“无效房间号;值必须介于1和“+MAX_ROOMS.ToString()”);
}
}
房间最大房间=空;
foreach(Room currentRoom in cRooms)//此循环通过房间数组(4)
{
//这是假设一个房间内的瓶数永远不会减少
如果((maxRoom==null)| |(maxRoom.BottleCount