C# 新手,并非所有代码路径都会返回值、瓶子计数程序、switch 语句
你好,我已经到了一个无法拔出的树桩。
我的程序记录了四个房间收集的瓶子数量。该程序应提示我输入房间号,然后输入该房间收集的瓶子数量。当用户输入“退出”时,程序将吐出每个房间收集的瓶子,并计算收集最多瓶子的房间。只要我没有输入退出,我应该能够向每个房间添加瓶子。
我无法让我的 GetRoom (int room) 工作,这是不返回值的方法。 我如何找到收集瓶子最多的房间?数学最大?
我无法使用 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();
}
}
}
}
Hello I've reached a stump that I cannot pull out.
My program records the number of bottles collected by four rooms. The program should prompt me to enter a room number and then how many bottles that room has collected. When ever the user types in "quit" the program will spit out the bottles collected by each room and calculate the room with the most bottles collected. I should be able to add bottles to each room as long as I haven't typed in quit.
I cannot get my GetRoom (int room) working, that is the method that does not return a value.
How would I find the room with the most bottles collected? Math.Max?
I cannot use LINQ or arrays. Its part of the assignment rules.
Heres is my code:
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();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要确保您的函数返回一些内容。您尝试过编译这段代码吗?它有一个编译错误,可以修复您的方法。
Math.Max 是找到最大值的好方法。
You need to make sure your function returns something. Have you tried compiling this code? It has a compile error that would fix your method.
And Math.Max is a good way to find the max.
问题是您的 GetRoom 方法被定义为返回 int ,因此它必须在每个代码路径上执行此操作。此特定示例不会在任何路径上返回值。
根据
GetRoom
方法中的逻辑,尽管您似乎正在修改房间而不是返回房间。如果是这种情况,只需将方法切换为返回void
The problem is that your
GetRoom
method is defined to return anint
and hence it must do so on every code path. This particular example doesn't return a value on any path.Based on the logic within the
GetRoom
method though it seems like you're modifying rooms instead of returning one. If that's the case simply switch the method to returnvoid
GetRoom
方法不返回值。在 switch 语句中提供默认值,或者在其后提供 return 语句。此外,您可以在这些情况下提出例外。示例:
顺便说一句,您可以使用 4 个元素的数组而不是 4 个不同的变量,这将简化您现有的代码并节省您编写新代码的时间。例如,
GetRoom
将如下所示:The
GetRoom
method does not return a value. Either provide a default value in a switch statement or and a return statement after that. Also, you can raise an exception in these cases.Example:
BTW, you can use an array of 4 elements instead of 4 different variables, that will simplify your existing code and save you some time writing new. For example,
GetRoom
will look like this:下面是一个使用类来保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必跟踪另一个数组,只需向类添加属性即可。
现在,各个房间都保存在列表中,而不是数组中,只是为了显示不同的结构。
这是新的 Room 类:
这是程序的新版本。请注意,添加了对最终用户输入的值的额外检查,以防止在尝试获取当前房间或将用户输入的值解析为 int 时出现异常:
Here's an example that uses a Class to hold the information for each room. The reason for using a class is so that if your program needs to change in the future to collect more information, you won't have to track yet another array, you can just add properties to the class.
The individual rooms are now held in a list instead of an array just to show a different construct.
Here is the new Room class:
And here is the new version of the program. Note that additional checking of the values entered by the end user has been added in order to prevent exceptions when trying to get the current room or parse to an int the value entered by the user:
您根本没有从您的函数中返回任何内容。尝试这样的方法,将结果存储在临时变量中,然后在退出函数时返回它。
You are not returning anything from your function at all. Try something like this, store your result in a temporary variable then when you exit the function return it.