对于C#来说,并不是所有的代码路径都返回一个值,瓶子计数程序,switch语句
本文关键字:一个 瓶子 语句 switch 程序 返回 并不是 来说 对于 路径 代码 | 更新日期: 2023-09-27 18:20:14
你好,我到达了一个无法拔出的树桩。
我的程序记录了四个房间收集的瓶子数量。程序应该提示我输入房间号码,然后输入该房间收集了多少瓶。当用户输入"退出"时,程序会吐出每个房间收集的瓶子,并计算收集瓶子最多的房间。只要我没有输入"退出",我应该可以在每个房间添加瓶子。
我无法使我的GetRoom(int-room)工作,这是一个不返回值的方法。我该如何找到收集瓶子最多的房间?数学。Max?
我不能使用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();
}
}
}
}
您需要确保您的函数返回一些东西。你试过编译这个代码吗?它有一个编译错误,可以修复您的方法。
Math.Max是找到最大的好方法
问题是GetRoom
方法被定义为返回int
,因此它必须在每个代码路径上都返回。这个特殊的例子在任何路径上都不会返回值。
根据GetRoom
方法中的逻辑,尽管您似乎在修改房间而不是返回房间。如果是这种情况,只需将方法切换为返回void
public void GetRoom() {
...
}
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
}
下面是一个使用class保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必跟踪另一个数组,只需向类添加属性即可。
各个房间现在被保存在一个列表中,而不是一个数组中,只是为了显示不同的结构。
这是新的房间等级:
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 + "!!!");
}
您根本没有从函数中返回任何内容。尝试这样的方法,将结果存储在一个临时变量中,然后当您退出函数时返回它
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;
}