C# 将数组转换为primative方法以表示相同的结果,使用no-LINQ-only int。
我的程序记录了四个房间在一个瓶子驱动器中收集的瓶子数量。当用户输入quit时,会显示每个房间收集的瓶子数量,并打印出瓶子最多的房间。我用了一个数组来记录房间号。如何更改方法而不是使用数组,我想启动room1、room2、room3、room4。如果我不使用数组,我是否能够使用循环数组调用来写行?这就是我的意思C# 将数组转换为primative方法以表示相同的结果,使用no-LINQ-only int。,c#,arrays,methods,int,C#,Arrays,Methods,Int,我的程序记录了四个房间在一个瓶子驱动器中收集的瓶子数量。当用户输入quit时,会显示每个房间收集的瓶子数量,并打印出瓶子最多的房间。我用了一个数组来记录房间号。如何更改方法而不是使用数组,我想启动room1、room2、room3、room4。如果我不使用数组,我是否能够使用循环数组调用来写行?这就是我的意思 int room = int.Parse(quit); Console.Write("Bottles collected in 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());
这一行:
}//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 < 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());
}
int maxValue=0;//启动赢家,构造函数从0开始
int maxRoomNumber=0;//启动赢得的房间号
for(int i=0;imaxValue)//确保在数组中拾取maxValue
{//正在查找房间号以便
maxValue=房间[i];
maxRoomNumber=i+1;
}//写不同房间收集的瓶子
WriteLine(“在{0}={1}房间收集的瓶子”,i+1,房间[i]);
}
//输出优胜者
WriteLine(“获胜者是房间”+maxRoomNumber+“!!!”);
}
}
}
谢谢你们,我很感激这个社区帮助我学习c语言 不能用单个变量轻松替换数组。如果你有这样的声明
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));
或者代替房间[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...
}
第二(也是更重要的):
代码完全不可扩展。如果您想添加第五个房间来收集瓶子,您必须完成整个程序并调整每个switch语句,这不仅耗时而且极易出错。下面是一个使用类保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必再跟踪另一个数组,只需向类添加属性即可
各个房间现在保存在一个列表中,而不是一个数组中,只是为了显示不同的构造
这是新的房间等级:
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)