English 中文(简体)
我如何超负荷使用该守则,使其接受我的入境?
原标题:How do I overload this code to make it accept my entry?

我正在制定一项方案,收集“游艇驾驶”的结果。 该方案将促使用户在1到4之间拥有一个房间号码,然后促使用户了解每个房间收集的瓶子数量。 用户应当选择“公平”类型,显示每个房间和所收集的瓶子数量。

Here is my code right now, I am having difficulty expanding the program to support multiple rooms.

namespace BottleDrive
{
  class Program
  {
    static void Main(string[] args)
    {
        int roomNumber;
        int numberOfBottles;
        char quit;

        Console.WriteLine("Enter the Room number you are in.");
        string roomSelect = "";
        roomSelect = Console.ReadLine();
        roomSelect = int.Parse(roomSelect);
        if (roomSelect >= 1)
        {
            Console.WriteLine("Enter the number of bottles collected by room one");
            string room1Bottles = "0";
            room1Bottles = Console.ReadLine();
            int room1 = int.Parse(room1Bottles);
            if (room1 == 1)
            {
                room1Bottles += room1;
                Console.WriteLine("Room one has " + room1 + " many bottles collected");
            }
        }
        if (Console.ReadLine() = quit)
        {
            Console.WriteLine("Room one has collected:" + room1Bottles + "
Room two has collected:" + room2Bottles + "Press space to quit");
            string systemExit = "";
            if (Console.ReadLine = "")
            {
                systemExit(0);
            }
        }
最佳回答

您可使用一个阵列(将使用房间号码计算指数),以追踪所收集的瓶颈数量:

int[] bottles = new int[4];

while (true)
{
    Console.Write("Enter the room you re in: ");
    string inputString = Console.ReadLine();
    if (inputString == "quit")
        break;
    int room = int.Parse(inputString);

    Console.Write("Bottles collected in room {0}: ", room);
    bottles[room - 1] = int.Parse(Console.ReadLine());
}

for (int i = 0; i < bottles.Length; ++i)
    Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, bottles[i]);

然后,一旦用户类型“空档”显示收集了多少瓶子,你就可以再次通过阵列进行消化。

问题回答

我怀疑你的法典只是因为这样编纂的:

string roomSelect = "";
roomSelect = Console.ReadLine();
roomSelect = int.Parse(roomSelect);

利用电梯变量储存舱位数。

由于你只需要知道每个房间一个变量,你可以使用一系列的分类,在这种情况下,有4个条目。 你们会从用户那里获得投入,并改用。 然后,你可以使用该数字来获取正确的阵列指数。

  int[] arr_rooms = new int[4];
 Console.WriteLine("Enter room number");
 int number = Int.Parse(Console.ReadLine());
 Console.WriteLine("enter number of bottles");
 int bottles = Int.Parse(Console.ReadLine());

 arr_rooms[number - 1] = bottles; //the -1 is because arrays start at 0

 Console.WriteLine("bottles in room 1 is " + arr_rooms[0].toString());

你们必须宣布一个储存瓶子的阵列:

static void Main(string[] args)
{
    const int NumberOfRooms = 4;

    int[] numberOfBottles = new int[NumberOfRooms];

    while (true) {
        Console.WriteLine("Enter the Room number you are in or  q  and <enter> to quit.");
        string line = Console.ReadLine();
        if (line.ToLower().StartsWith("q")) {
            break;
        }
        int roomNumber = int.Parse(line) - 1;
        if (roomNumber >= 0 && roomNumber < NumberOfRooms) {
            Console.WriteLine("Enter the number of bottles collected by room number " + (roomNumber + 1));
            line = Console.ReadLine();
            int bottles = int.Parse(line);
            numberOfBottles[roomNumber] += bottles;
            Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[roomNumber], roomNumber + 1);
            Console.WriteLine();
        }
    }
    Console.WriteLine();
    for (int i = 0; i < NumberOfRooms; i++) {
        Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[i], i + 1);
    }
    Console.WriteLine("Press any key to quit.");
    Console.ReadKey();
}

请注意,阵列的索引从0到1项。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签