【发布时间】:2012-02-12 12:12:12
【问题描述】:
我的程序收集四个房间收集的瓶子数量。当用户在任何时候输入退出时,程序退出while 外观并显示瓶子收集的结果。然后它会计算出瓶数最多的获胜房间。
我有一个while循环,我不明白为什么我不能进入它。要进入 while 循环,它会提示我输入一个数字 1-4,就像我的数组中使用的那样,以及每个房间收集的瓶子数量。如果我在任何时候输入退出,程序将停止记录瓶子并吐出结果和瓶子最多的获胜房间。
如何在我输入瓶数之前先显示“输入您所在的房间号”?我的问题存在于 getBottles()
我认为这条线不能用在数组中,对吗?
房间[房间 - 1] += int.Parse(Console.ReadLine());
namespace BottleDrive
{
class BottleDrive
{
public int[] rooms;
public BottleDrive()
{
rooms = new int[4];
}
static void Main(string[] args) //static is member of class not object
{
BottleDrive bD = new BottleDrive();
bD.getBottles();
bD.displayBottleCount();
bD.findWinner();
}
public void getBottles()
{
string quit = Console.ReadLine();
while while(quit != "quit")
{
int room = int.Parse(quit);
Console.Write("Bottles collected in room {0}: ", room);
rooms[room - 1] += int.Parse(Console.ReadLine());
Console.Write("Enter the room you're in: ");
}
}
public void findWinner()
{
int maxValue = 0;//initiates the winner, contructor starts at 0
int maxRoomNumber = 0;//initiates the room number that wins
for (int i = 0; i < rooms.Length; ++i)//This loop goes through the array of rooms (4)
{
if (rooms[i] > maxValue)//Makes sure that the maxValue is picked in the array
{//Looking for room number for the
maxValue = rooms[i];
maxRoomNumber = i + 1;
}
}
Console.WriteLine("And the Winner is room " + maxRoomNumber + "!!!");
}
public void displayBottleCount()
{
Console.WriteLine("Bottles collected in room one: " + rooms[0]);
Console.WriteLine("Bottles collected in room two: " + rooms[1]);
Console.WriteLine("Bottles collected in room three: " + rooms[2]);
Console.WriteLine("Bottles collected in room four: " + rooms[3]);
}
}
}
【问题讨论】:
-
确保您的套管匹配。
while (quit.ToLower() == "quit")
标签: c# arrays loops while-loop