【发布时间】:2012-01-17 01:38:06
【问题描述】:
我的程序应该统计 4 个房间的瓶子。当用户输入退出时,程序会显示每个房间收集了多少瓶。
程序提示用户时出现问题,它提示两次但将最后输入的数字作为选择的roomNumber。我也不知道如何设置我的 if 语句,以便我可以不断地将用户输入的瓶子添加到每个特定房间。
我可以使用一个临时变量来存储输入的瓶数,然后将其添加到保存当前瓶数的room1、room2吗?
namespace BottleDrive1 {
class Program {
static void Main(string[] args)
{
//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
int roomNumber;
while (true)
{
Console.WriteLine("Enter the the room number you are in.");
string quit = Console.ReadLine();
if (quit == "quit")
{
//Break statement allows quit to jump out of loop
break;
}
//int roomT = int.Parse(quit);//a temp variable I want to use to add bottles to the count.
roomNumber = int.Parse(Console.ReadLine());
if (roomNumber == 1)
{
Console.WriteLine("How many bottles did room 1 collect?");
room1 = int.Parse(Console.ReadLine());
}
if (roomNumber == 2)
{
Console.WriteLine("How many bottles did room 2 collect?");
room2 = int.Parse(Console.ReadLine());
}
if (roomNumber == 3)
{
Console.WriteLine("How many bottles did room 3 collect?");
room3 = int.Parse(Console.ReadLine());
}
if (roomNumber == 4)
{
Console.WriteLine("How many bottles did room 4 collect?");
room4 = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Bottles each room has collected:");
Console.WriteLine("Room one:" + room1);
Console.WriteLine("Room two:" + room2);
Console.WriteLine("Room three:" + room3);
Console.WriteLine("Room four:" + room4);
int maxRoom = room1;
if (room2 > maxRoom)
{
maxRoom = 2;
}
if (room3 > maxRoom)
{
maxRoom = 3;
}
if (room4 > maxRoom)
{
maxRoom = 4;
}
else
{
maxRoom = 1;
}
Console.WriteLine("The winner is room " + maxRoom + "!");
}
}
}
【问题讨论】:
-
查找 Dictionary
(一种通用结构)并思考如何帮助您做到这一点。