C#的新手,并非所有代码路径都返回一个值,瓶子计数程序,开关语句

时间:2012-01-15 00:12:53

标签: c# debugging switch-statement

你好,我已经到了一个我无法退出的树桩。

我的程序记录了四个房间收集的瓶子数量。该程序应该提示我输入房间号码,然后输入该房间收集的瓶子数量。当用户键入“退出”时,程序将吐出每个房间收集的瓶子,并计算收集最多瓶子的房间。只要我没有输入戒烟,我应该能够为每个房间添加瓶子。

我无法使我的GetRoom(int room)工作,这是不返回值的方法。 如何找到收集最多瓶子的房间? Math.Max?

我无法使用LINQ或数组。它是分配规则的一部分。 Heres是我的代码:

namespace BottleDrive1
{
    class Program
    {//Initialize 4 rooms. 
        int room1 = 0;

        int room2 = 0;

        int room3 = 0;

        int room4 = 0;

        static void Main(string[] args)
        {
            //Start of while loop to ask what room your adding into. 
            while (true)
            {
                Console.Write("Enter the room you're in: ");
                //If user enters quit at anytime, the code will jump out of while statement and enter for loop below
                string quit = Console.ReadLine();
                if (quit == "quit")
                    //Break statement allows quit to jump out of loop
                    break;
            }
        }

        private void SetRoom(int room, int value)
        {
            switch (room)
            {
                case 1:
                    room1 = value;
                    break;
                case 2:
                    room2 = value;
                    break;
                case 3:
                    room3 = value;
                    break;
                case 4:
                    room4 = value;
                    break;
            }
        }
        public void GetRoom(int room)
        {
            int count = int.Parse(Console.ReadLine());

            switch (room)
            {
                case 1:
                    room1 += count;
                    break;
                case 2:
                    room2 += count;
                    break;
                case 3:
                    room3 += count;
                    break;
                case 4:
                    room4 += count;
                    break;
                default:
                    throw new ArgumentException();
            }

        }
    }
}

5 个答案:

答案 0 :(得分:1)

你需要确保你的函数返回一些东西。您是否尝试过编译此代码?它有一个编译错误,可以修复你的方法。

Math.Max是找到最大值的好方法。

答案 1 :(得分:1)

GetRoom方法不返回值。在switch语句中提供默认值,或者在此之后提供return语句。此外,您可以在这些情况下引发异常。

示例:

public int GetRoom(int room)
{
    int count = int.Parse(Console.ReadLine());
    switch (room)
    {
        case 1:
            room1 += count;
            break;
        case 2:
            room2 += count;
            break;
        case 3:
            room3 += count;
            break;
        case 4:
            room4 += count;
            break;
        default:
            throw new ArgumentException(); //either this
    }
    throw new ArgumentException(); //or this
}
BTW,您可以使用4个元素的数组而不是4个不同的变量,这将简化您现有的代码并节省您编写新代码的时间。例如,GetRoom将如下所示:

public int GetRoom(int room)
{
    int count = int.Parse(Console.ReadLine());
    rooms[room] += count;
    //return what you need to return here
}

答案 2 :(得分:1)

问题是您的GetRoom方法被定义为返回int,因此必须在每个代码路径上执行此操作。此特定示例不会在任何路径上返回值。

基于GetRoom方法中的逻辑,虽然看起来你正在修改房间而不是返回房间。如果是这种情况,只需将方法切换为返回void

public void GetRoom() {
  ...
}

答案 3 :(得分:0)

你根本没有从你的功能中返回任何东西。尝试这样的事情,将结果存储在临时变量中,然后在退出函数时返回它。

public int GetRoom(int room)
{
    int count = int.Parse(Console.ReadLine());
    int temp = 0;
    switch (room)
    {
        case 1:
            room1 += count;
            temp = room1;
            break;
        case 2:
            room2 += count;
            temp = room2;
            break;
        case 3:
            room3 += count;
            temp = room3;
            break;
        case 4:
            room4 += count;
            temp = room4;
            break;
        default:
            throw new ArgumentException();
    }

    return temp;

} 

答案 4 :(得分:0)

这是一个使用Class来保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,您将不必跟踪另一个数组,您只需向该类添加属性。

现在,单个房间被保存在列表中而不是数组中,只是为了显示不同的结构。

这是新的Room类:

public class Room
{
    public int Number { get; set; }
    public int BottleCount { get; set; }

    public Room(int wNumber)
    {
        Number = wNumber;
    }
}

这是该程序的新版本。请注意,添加了对最终用户输入的值的附加检查,以防止在尝试获取当前空间或将用户输入的值解析为int时出现异常:

    static void Main(string[] args)
    {
        const int MAX_ROOMS = 4;
        var cRooms = new System.Collections.Generic.List<Room>();

        for (int nI = 0; nI < MAX_ROOMS; nI++)
        {
            // The room number is 1 to 4
            cRooms.Add(new Room(nI + 1));
        }

        // Initializes the room that wins
        //Start of while loop to ask what room your adding into. 
        while (true)
        {
            Console.Write("Enter the room you're in: ");
            //If user enters quit at anytime, the code will jump out of while statement and enter for loop below
            string roomNumber = Console.ReadLine();
            if (roomNumber == "quit")
            {
                //Break statement allows quit to jump out of loop
                break;
            }
            int room = 0;
            if (int.TryParse(roomNumber, out room) && (room < MAX_ROOMS) && (room >= 0)) {
                Room currentRoom;

                currentRoom = cRooms[room];

                Console.Write("Bottles collected in room {0}: ", currentRoom.Number);

                int wBottleCount = 0;

                if (int.TryParse(Console.ReadLine(), out wBottleCount) && (wBottleCount >= 0))
                {
                    // This line adds the count of bottles and records it so you can continuously count the bottles collected.
                    currentRoom.BottleCount += wBottleCount;
                }
                else
                {
                    Console.WriteLine("Invalid bottle count; value must be greater than 0");
                }
            }
            else
            {
                Console.WriteLine("Invalid room number; value must be between 1 and " + MAX_ROOMS.ToString());
            }
        }

        Room maxRoom = null;

        foreach (Room currentRoom in cRooms) //This loop goes through the array of rooms (4)
        {
            // This assumes that the bottle count can never be decreased in a room
            if ((maxRoom == null) || (maxRoom.BottleCount < currentRoom.BottleCount))
            {
                maxRoom = currentRoom;
            }
            Console.WriteLine("Bottles collected in room {0} = {1}", currentRoom.Number, currentRoom.BottleCount);
        }
        //Outputs winner
        Console.WriteLine("And the Winner is room " + maxRoom.Number + "!!!");
    }