转换数组一颗灵长类动物的方法前preSS相同的结果,不使用LINQ仅INT。 [英] Convert Array into a primative method to express the same result, using no LINQ only int.

查看:126
本文介绍了转换数组一颗灵长类动物的方法前preSS相同的结果,不使用LINQ仅INT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序记录瓶四个房间装在瓶子里的驱动器收集的数目。当用户键入退出,每个房间都有收集显示瓶子的数量,以及它打印出具有最大瓶的房间。我已经使用的阵列来跟踪房间号。如何改变的,而不是使用数组的方法,我想发起ROOM1,会议室2,会议室3,会议室4。我将能够使用我的环状阵列电话如果我不使用数组来写行?还有我的意思行。

my program records the number of bottles four rooms has collected in a bottle drive. When the user types in quit, the number of bottle each room has collected is shown as well as it prints out the room that has the most bottles. I have used an array to keep track of room number. How can change a method of instead of using an array, I would like to initiate room1, room2, room3, room4. Will i be able to use my looped array calls to write lines if i dont use an array? There are the lines i mean.

            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]);

下面是我的code:

         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 + "!!!");

    }
}
 }

谢谢你,我AP preciate这个社会有多少帮助我学习C锋利。

And thank you, I appreciate how much this community has helped me learn c sharp.

推荐答案

您不能很容易地与各个变量代替数组。如果你有一个像

You can not replace an array easily with individual variables. If you have a declaration like

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

和要访问的房间号 I 然后你必须写

and want to access room number i then you have to write

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]);


如果你真的想要走这阵列更低的方式,我建议你使用辅助方法:


If you really want to go this array-less way, I suggest you to use helper methods:

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到会议室4为类的成员,使这项工作。

You must declare variables room1 to room4 as class members to make this work.

现在你可以写:

Console.WriteLine(GetRoom(i));

或代替客房[I] + = N;

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

这篇关于转换数组一颗灵长类动物的方法前preSS相同的结果,不使用LINQ仅INT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆