【问题标题】:How do I writeline before prompt in my while loop?如何在我的 while 循环中提示之前写行?
【发布时间】:2012-02-12 12:12:12
【问题描述】:

我的程序收集四个房间收集的瓶子数量。当用户在任何时候输入退出时,程序退出while 外观并显示瓶子收集的结果。然后它会计算出瓶数最多的获胜房间。

我有一个while循环,我不明白为什么我不能进入它。要进入 while 循环,它会提示我输入一个数字 1-4,就像我的数组中使用的那样,以及每个房间收集的瓶子数量。如果我在任何时候输入退出,程序将停止记录瓶子并吐出结果和瓶子最多的获胜房间。

如何在我输入瓶数之前先显示“输入您所在的房间号”?我的问题存在于 getBottles()

我认为这条线不能用在数组中,对吗?

房间[房间 - 1] += int.Parse(Console.ReadLine());

namespace BottleDrive
{
    class BottleDrive
    {
        public int[] rooms;
        public BottleDrive()
        {
            rooms = new int[4];
        }

        static void Main(string[] args) //static is member of class not object
        {
            BottleDrive bD = new BottleDrive();
            bD.getBottles();
            bD.displayBottleCount();
            bD.findWinner();
        }
        public void getBottles()
        {
            string quit = Console.ReadLine();
            while while(quit != "quit")
            {
                int room = int.Parse(quit);

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

                Console.Write("Enter the room you're in: ");
            }
        }
        public void findWinner()
        {
            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;
                }
            }
            Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
        }
        public void displayBottleCount()
        {
            Console.WriteLine("Bottles collected in room one: " + rooms[0]);
            Console.WriteLine("Bottles collected in room two: " + rooms[1]);
            Console.WriteLine("Bottles collected in room three: " + rooms[2]);
            Console.WriteLine("Bottles collected in room four: " + rooms[3]);
        }
    }
}

【问题讨论】:

  • 确保您的套管匹配。 while (quit.ToLower() == "quit")

标签: c# arrays loops while-loop


【解决方案1】:

你应该有while(quit != "quit")作为while条件吗?

【讨论】:

    【解决方案2】:
    while (quit == "quit")
    

    如果退出(你从控制台得到的)是“退出”,上面的行只会运行循环。

    你想要:

    while (quit != "quit")
    

    while (!quit.Equals("quit"))
    

    毕竟,尽管您实际上也没有在循环内更新退出,所以一旦进入,您将永远不会退出。

    您需要捕获您的控制台。Readline 并将其置于“退出”状态。

    您可能还想查看int.TryParse,以防人们输入的字符串不是退出或有效整数。 TryParse 会告诉你解析是否成功,而不是抛出异常。

    【讨论】:

      【解决方案3】:

      这一行:

      while (quit == "quit")
      

      实际上应该是:

      while (quit != "quit")
      

      或者更好:

      while (!quit.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
      

      这将忽略输入的大小写。正如其他人所指出的,您的循环存在更多问题。尝试将其用于您的 getBottles 函数:

      public void getBottles()
      {
          string input;
      
          do
          {
              Console.Write("Enter the room you're in: (or quit)");
              input = Console.ReadLine();
      
              int room;
              // doing try parst because the input might be "quit" or other junk
              if (int.TryParse(input, out room))
              {
                  Console.Write("Bottles collected in room {0}: ", room);
                  // this will fail hard if the input is not of type int
                  rooms[room - 1] += int.Parse(Console.ReadLine());
              }
          } while (!input.Equals("quit", StringComparison.CurrentCultureIgnoreCase)); 
      }
      

      【讨论】:

      • 谢谢,do 语句是要走的路,然后嵌套while。太棒了!
      【解决方案4】:

      试试:

      while (!quit.Equals("quit"))
      

      【讨论】:

        【解决方案5】:

        getBottles() 函数看起来很奇怪。只有在键入“quit”时才进入 while 循环。我不认为这是你想要的行为。

        【讨论】:

        • 是的,我刚才提到了,我想让它提示我先输入房间号,然后再输入那个房间收集的瓶子。 getBottles() ,是格式错误还是房间代码[room - 1] += int.Parse(Console.ReadLine());错了吗?
        猜你喜欢
        • 2014-01-21
        • 2016-04-02
        • 2019-09-03
        • 2021-11-25
        • 2017-12-17
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        相关资源
        最近更新 更多