【问题标题】:How do I overload this code to make it accept my entry?如何重载此代码以使其接受我的输入?
【发布时间】:2012-01-12 06:14:21
【问题描述】:

我正在开发一个程序来收集“瓶驱动器”的结果。程序会提示用户输入房间号,介于 1 和 4 之间,然后提示用户每个房间收集的瓶子数量。用户应该可以选择键入“退出”以显示每个房间和收集的瓶子数量。

这是我现在的代码,我在扩展程序以支持多个房间时遇到了困难。

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 + "\nRoom two has collected:" + room2Bottles + "Press space to quit");
            string systemExit = "";
            if (Console.ReadLine = "")
            {
                systemExit(0);
            }
        }

【问题讨论】:

  • 我在弄清楚您要问的确切内容时遇到了一些麻烦,但我已尽力解决您的问题。更有针对性的问题(我在x 上遇到问题)比仅仅发布您的代码并要求我们修复它要好得多,所以如果您有一个具体的问题,这是一个很好的起点。
  • 由于您是 C# 新手,您可能想熟悉的东西是 try and catch。例如,您可能希望将 int.Parse() 包装在 try catch 块中,以防用户输入不适合整数的内容。应用程序不会高兴。
  • 我不明白为什么这里有一个C 标签。

标签: c# debugging overloading


【解决方案1】:

您可以使用一个数组(房间号将用于计算索引)来跟踪收集的瓶子数量:

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

然后,一旦用户键入“退出”以显示收集了多少瓶子,您就可以再次遍历数组。

【讨论】:

  • 很酷的循环,如何保存我输入的数字中的数据?
  • 数据存储在数组中。我更新了代码以显示输出。
  • 如果我提交我肯定会因为抄袭而受到惩罚,那已经超出了我的水平。但我可以毫不犹豫地把知识从它身上拿走。
  • 如何让代码暂停以显示瓶数。或者只是我,我在 Visual Studios Debug/Release 上运行它
  • 您可以在显示瓶数的循环之后添加一个 Console.ReadLine()。或者你可以使用 CTRL+F5 来运行程序而不是调试它。
【解决方案2】:

我怀疑你的代码仅仅因为这个而编译:

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

使用一个整型变量来存储解析后的房间号。

【讨论】:

    【解决方案3】:

    因为您只需要知道每个房间的一个变量,您可以使用一个整数数组,在本例中是一个包含 4 个条目的数组。您将从用户那里获得输入并转换为 int。然后,您可以使用该数字访问正确的数组索引。

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

    【讨论】:

      【解决方案4】:

      您必须声明一个数组来存储瓶子的数量:

      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 到项目数减一。

      【讨论】:

        猜你喜欢
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 2017-01-10
        • 1970-01-01
        • 2020-12-19
        • 2020-11-10
        • 2015-07-15
        相关资源
        最近更新 更多