对于C#,并非所有代码路径都返回值、瓶子计数程序、开关语句

对于C#,并非所有代码路径都返回值、瓶子计数程序、开关语句,c#,debugging,switch-statement,C#,Debugging,Switch Statement,你好,我已经到了一个无法拔出的树桩 我的程序记录了四个房间收集的瓶子数量。程序应该提示我输入房间号,然后输入房间收集了多少瓶子。当用户输入“退出”时,程序将吐出每个房间收集的瓶子,并计算收集瓶子最多的房间。只要我没有输入quit,我应该可以在每个房间里添加瓶子 我无法让我的GetRoom(introom)工作,这是一个不返回值的方法。 我如何找到收集瓶子最多的房间?数学,马克斯 我不能使用LINQ或数组。这是分配规则的一部分。 这是我的密码: namespace BottleDrive1 {

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

我的程序记录了四个房间收集的瓶子数量。程序应该提示我输入房间号,然后输入房间收集了多少瓶子。当用户输入“退出”时,程序将吐出每个房间收集的瓶子,并计算收集瓶子最多的房间。只要我没有输入quit,我应该可以在每个房间里添加瓶子

我无法让我的GetRoom(introom)工作,这是一个不返回值的方法。 我如何找到收集瓶子最多的房间?数学,马克斯

我不能使用LINQ或数组。这是分配规则的一部分。 这是我的密码:

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

        }
    }
}

您需要确保函数返回一些内容。你试过编译这段代码吗?它有一个编译错误,可以修复您的方法


Max是找到Max的一个好方法。

您需要确保函数返回一些内容。你试过编译这段代码吗?它有一个编译错误,可以修复您的方法


Max是找到Max的好方法。

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
}
顺便说一句,您可以使用4个元素的数组,而不是4个不同的变量,这将简化现有代码并节省编写新代码的时间。例如,
GetRoom
将如下所示:

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

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
}
顺便说一句,您可以使用4个元素的数组,而不是4个不同的变量,这将简化现有代码并节省编写新代码的时间。例如,
GetRoom
将如下所示:

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

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

基于
GetRoom
方法中的逻辑,尽管看起来您是在修改房间而不是返回房间。如果是这种情况,只需切换方法返回
void

public void GetRoom() {
  ...
}

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

基于
GetRoom
方法中的逻辑,尽管看起来您是在修改房间而不是返回房间。如果是这种情况,只需切换方法返回
void

public void GetRoom() {
  ...
}

您根本没有从函数返回任何内容。尝试类似的方法,将结果存储在临时变量中,然后在退出函数时返回它

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;

} 

您根本没有从函数返回任何内容。尝试类似的方法,将结果存储在临时变量中,然后在退出函数时返回它

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;

} 

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

各个房间现在保存在一个列表中,而不是一个数组中,只是为了显示不同的构造

这是新的房间等级:

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 + "!!!");
    }
static void Main(字符串[]args)
{
const int MAX_房间=4间;
var cRooms=new System.Collections.Generic.List();
对于(int-nI=0;nI=0)){
房间当前房间;
currentRoom=cRooms[房间];
Console.Write(“在{0}房间收集的瓶子:”,currentRoom.Number);
int wBottleCount=0;
if(int.TryParse(Console.ReadLine(),out wBottleCount)&&(wBottleCount>=0))
{
//此行添加瓶子数并记录,以便您可以连续计数收集的瓶子。
currentRoom.BottleCount+=wBottleCount;
}
其他的
{
Console.WriteLine(“无效瓶数;值必须大于0”);
}
}
其他的
{
Console.WriteLine(“无效房间号;值必须介于1和“+MAX_ROOMS.ToString()”);
}
}
房间最大房间=空;
foreach(Room currentRoom in cRooms)//此循环通过房间数组(4)
{
//这是假设一个房间内的瓶数永远不会减少
如果((maxRoom==null)| |(maxRoom.BottleCount