在while循环中,如何在提示前写入行

本文关键字:提示 while 循环 | 更新日期: 2023-09-27 18:20:29

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

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

在我输入瓶子数量之前,如何让"输入您所在的房间号码"首先出现?我的问题存在于getBottles()中

我想这行不能用在数组中,对吗?

rooms[room-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循环中,如何在提示前写入行

while (quit == "quit")

只有当quit(您从控制台得到的)是"quit"时,上面的行才会运行循环。

您想要:

while (quit != "quit")

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

毕竟,尽管你也没有真正更新,但在你的循环中退出,所以一旦进入,你就永远不会退出。

您需要捕获您的控制台。重新排列并将其放入"退出"。

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

此行:

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

您是否应该为while条件设置while(quit != "quit")

尝试:

while (!quit.Equals("quit"))
函数getBottles()看起来非常奇怪。只有当您键入"退出"时,才能进入while循环。我不认为这是你想要的行为。