如何在我的while循环中提示之前写信?

时间:2012-01-17 17:18:00

标签: c# arrays loops while-loop

我的程序收集了四个房间收集的瓶子数量。当用户随时键入退出时,程序会不时显示并显示瓶子收集的结果。然后计算具有最多瓶数的获胜房间。

我有一个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]);
        }
    }
}

5 个答案:

答案 0 :(得分:4)

while (quit == "quit")

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

你想:

while (quit != "quit")

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

毕竟,虽然你实际上并没有在你的循环中更新quit,所以一旦进入你就永远不会离开。

您需要捕获您的console.Readline并将其放入“退出”。

如果用户输入的字符串不是退出或有效整数,您可能还需要查看int.TryParse。 TryParse会告诉你解析是否成功,而不是抛出异常。

答案 1 :(得分: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)); 
}

答案 2 :(得分:1)

你是否应该为{while}条件while(quit != "quit")

答案 3 :(得分:0)

尝试:

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

答案 4 :(得分:0)

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