2012-01-16 131 views
1

我的程序应该是从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 + "!"); 
     } 
    } 
} 
+0

查找字典(一个通用结构),并认为这可以帮助你做到这一点。 – 2012-01-16 17:28:30

+0

好吧,生病了 – gli 2012-01-16 17:42:55

回答

4

你非常接近!在你的while循环中,你阅读从控制台退出的值,并通过再次从控制台读取一次,“提示”用户再次输入房间号码。您可以通过简单地获取quit值和解析房间号来避免第二个“提示”。请注意,一切都将正常工作,因为如果用户进入退出,那么你将退出反正循环:

while (true) 
{ 
    Console.WriteLine("Enter the the room number you are in."); 
    string quit = Console.ReadLine(); 

    // another quick thing to fix is to ignore the case (don't trust the user) 
    if(quit .Equals("quit", StringComparison.InvariantCultureIgnoreCase)) 
    { 
     //Break statement allows quit to jump out of loop 
     break; 
    } 

    roomNumber = int.Parse(quit); // you've already asked the user for input, so just reuse the variable holding the input 

    // TODO: consider what happens if you can't parse an integer, i.e. use TryParse etc 

    // do whatever you need to do after that 
} 

后你拿到房号,然后不要忘了与添加瓶的数量“ + =运营商',例如:

room4 += int.Parse(Console.ReadLine()); 
0

您需要添加从到Console.ReadLine()收到瓶子的现有数量价值一个房间,像这样:

room1 = room1 + int.Parse(Console.ReadLine()); 

或者你可以使用简写版本:

room1 += int.Parse(Console.ReadLine()); 

您目前遇到的问题是,当您为房间1输入5个瓶子时room1将5个保存在变量中。下一次您输入10瓶的房间room1得到10保存在变量。您需要将这两个数字加在一起以保留一个房间的瓶子总数。

相关问题