瓶子计数程序,switch语句查询,求最大数量
我的程序记录了 4 个房间收集的瓶子数量。我是 C# 的初学者,但以前学过 java。
我不会使用 LINQ,也不会使用数组。仅 switch 语句(抱歉,我知道它效率低下)
我的程序必须记录用户输入的瓶子数量,当用户输入“退出”时,程序应该吐出所有房间的瓶子数量并确定房间瓶数最多的为获胜者。
我陷入了这个 switch 语句,我找不到启动我的房间(room1、room2、room3、room4)的方法,它说变量 room1-4 未分配。我应该能够通过使用开关连续向房间添加瓶子。
当我输入 quit 时,程序可以吐出房间收集的所有瓶子,并找到瓶子最多的房间。
感谢您抽出宝贵的时间,我很感激这个社区对我的帮助。
namespace BottleDrive1
{
class Program
{
static void Main(string[] args)
{//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
//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 int GetRoom(int room)
{
int count = int.Parse(Console.ReadLine());
switch (room)
{
case 1:
room1 += count;
break;
case 2:
room2 += count;
case 3:
room3 += count;
break;
case 4:
room4 += count;
break;
}
}
}
//This for statement lists the 4 rooms and their bottle count when the user has entered quit. An alternative to below
/*for (int i = 0; i < rooms.Length; ++i)
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);*/
/* int maxValue = 0;//initiates the winner, contructor starts at 0
int maxRoomNumber = 0;//initiates the room number that wins
for (int i = 0; i < room[i].Length; ++i)//This loop goes through the array of rooms (4)
{
if (room[i] > maxValue)//Makes sure that the maxValue is picked in the array
{//Looking for room number for the
maxValue = room[i];
maxRoomNumber = i + 1;
}//Writes the bottles collected by the different rooms
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);
}
//Outputs winner
Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
*/
}
程序的最后一部分是我尝试找到最大数量,因为我使用了一个数组来启动。我不能使用数组。
my program records the number of bottles that 4 rooms has collected. I am a beginner at C# but have done java in the past.
I shall not use LINQ, I shall not use Arrays. Switch statement only (sorry, i know its inefficient)
My program has to record the number of bottles entered by the user and when the user types "quit" the program is suppose to spit out the bottle count of all rooms and determine the room with the most bottle as the winner.
I'm stuck on this switch statement which I cannot find a way to initiate my rooms (room1, room2, room3, room4), it says variable room1-4 is not assigned. I should be able to continuously add bottle to a room through using the switch.
When i type quit, the program can spit out all the bottles collected by rooms and find the room with the most bottles.
Thanks for your time, I appreciate how much this community has helped me.
namespace BottleDrive1
{
class Program
{
static void Main(string[] args)
{//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
//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 int GetRoom(int room)
{
int count = int.Parse(Console.ReadLine());
switch (room)
{
case 1:
room1 += count;
break;
case 2:
room2 += count;
case 3:
room3 += count;
break;
case 4:
room4 += count;
break;
}
}
}
//This for statement lists the 4 rooms and their bottle count when the user has entered quit. An alternative to below
/*for (int i = 0; i < rooms.Length; ++i)
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);*/
/* int maxValue = 0;//initiates the winner, contructor starts at 0
int maxRoomNumber = 0;//initiates the room number that wins
for (int i = 0; i < room[i].Length; ++i)//This loop goes through the array of rooms (4)
{
if (room[i] > maxValue)//Makes sure that the maxValue is picked in the array
{//Looking for room number for the
maxValue = room[i];
maxRoomNumber = i + 1;
}//Writes the bottles collected by the different rooms
Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, rooms[i]);
}
//Outputs winner
Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
*/
}
The last part of the program was my attempted to find max number, since i used an array to start. I must not use an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
下面是一个使用类来保存每个房间信息的示例。使用类的原因是,如果您的程序将来需要更改以收集更多信息,则不必跟踪另一个数组,只需向类添加属性即可。
现在,各个房间都保存在列表中,而不是数组中,只是为了显示不同的结构。
这是新的 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 + "!!!");
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
只需将 int 声明移至 Main 方法上方,如下所示:
Just move the int declarations above the Main method like this :