如何将数据添加到创建的变量中

本文关键字:创建 变量 添加 数据 | 更新日期: 2023-09-27 18:20:22

我的程序应该统计来自4个房间的瓶子。当用户输入quit时,程序会吐出每个房间收集了多少瓶子。

当程序提示用户时,我遇到了一个问题,它提示了两次,但将最后输入的数字作为所选的roomNumber。我也不知道如何设置if语句,这样我就可以不断地将用户输入的瓶子添加到每个特定的房间。

我可以使用一个临时变量来存储输入的瓶子数量,然后将其添加到保存当前瓶子数量的room1room2中吗?

namespace BottleDrive1 {
    class Program {
        static void Main(string[] args)
        {
            //Initialize 4 rooms. 
            int room1 = 0;
            int room2 = 0;
            int room3 = 0;
            int room4 = 0;
            int roomNumber;
            while (true)
            {
                Console.WriteLine("Enter the the room number you are in.");
                string quit = Console.ReadLine();
                if (quit == "quit")
                {
                    //Break statement allows quit to jump out of loop
                    break;
                }
                //int roomT = int.Parse(quit);//a temp variable I want to use to add bottles to the count.
                roomNumber = int.Parse(Console.ReadLine());
                if (roomNumber == 1)
                {
                    Console.WriteLine("How many bottles did room 1 collect?");
                    room1 = int.Parse(Console.ReadLine());
                }
                if (roomNumber == 2)
                {
                    Console.WriteLine("How many bottles did room 2 collect?");
                    room2 = int.Parse(Console.ReadLine());
                }
                if (roomNumber == 3)
                {
                    Console.WriteLine("How many bottles did room 3 collect?");
                    room3 = int.Parse(Console.ReadLine());
                }
                if (roomNumber == 4)
                {
                    Console.WriteLine("How many bottles did room 4 collect?");
                    room4 = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Bottles each room has collected:");
            Console.WriteLine("Room one:" + room1);
            Console.WriteLine("Room two:" + room2);
            Console.WriteLine("Room three:" + room3);
            Console.WriteLine("Room four:" + room4);
            int maxRoom = room1;
            if (room2 > maxRoom)
            {
                maxRoom = 2;
            }
            if (room3 > maxRoom)
            {
                maxRoom = 3;
            }
            if (room4 > maxRoom)
            {
                maxRoom = 4;
            }
            else
            {
                maxRoom = 1;
            }
            Console.WriteLine("The winner is room " + maxRoom + "!");
        }
    }
}

如何将数据添加到创建的变量中

你非常接近!在while循环中,您从控制台读取退出的值,并通过再次从控制台读取再次"提示"用户输入房间号。您可以通过简单地获取quit的值并解析房间号来避免第二个"提示"。注意,一切都会很好,因为如果用户输入退出,那么你无论如何都会退出循环:

while (true) 
{
    Console.WriteLine("Enter the the room number you are in.");
    string quit = Console.ReadLine();
    // another quick thing to fix is to ignore the case (don't trust the user)
    if(quit .Equals("quit", StringComparison.InvariantCultureIgnoreCase))
    {
        //Break statement allows quit to jump out of loop
        break;
    }
    roomNumber = int.Parse(quit); // you've already asked the user for input, so just reuse the variable holding the input
    // TODO: consider what happens if you can't parse an integer, i.e. use TryParse etc
    // do whatever you need to do after that
}

在你得到房间号码后,不要忘记用"+=运算符"添加瓶子的数量,例如:

room4 += int.Parse(Console.ReadLine());

您需要将从Console.ReadLine()收到的值添加到一个房间的现有瓶子数量中,如下所示:

room1 = room1 + int.Parse(Console.ReadLine());

或者,您可以使用缩写版本:

room1 += int.Parse(Console.ReadLine());

您目前遇到的问题是,当您为房间输入5瓶时,一个room1会在变量中保存5瓶。下次为房间1输入10瓶时,room1会在变量中保存10瓶。你需要把这两个数字加在一起,才能保持一个房间的瓶子总数。