【问题标题】:How can I add data to a created variable?如何将数据添加到创建的变量?
【发布时间】:2012-01-17 01:38:06
【问题描述】:

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

程序提示用户时出现问题,它提示两次但将最后输入的数字作为选择的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 + "!");
        }
    }
}

【问题讨论】:

  • 查找 Dictionary(一种通用结构)并思考如何帮助您做到这一点。

标签: c# methods


【解决方案1】:

你很亲密!在您的 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());

【讨论】:

    【解决方案2】:

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

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

    您也可以使用速记版本:

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多