My program is supposed to tally up bottles from 4 rooms. When the user types in quit, the program spits out how many bottles each room has collected.
I have a problem when the program prompts the user, it prompts twice but takes the last entered number as the selected roomNumber. I also do not know how to set up my if statements so I can continuously add up the bottles entered by the user to each specific room.
Can I use a temporary variable to store the entered bottle count then add it to the room1, room2 which holds the current bottle count number?
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 + "!");
}
}
}
You were very close! In your while loop, you read the value for quit from console and you “prompt” the user to enter the room number again by reading from console one more time. You can avoid the second “prompt” by simply taking the value of quit and parsing the room number. Note that everything will work fine, because if the user enters quit, then you will exit the loop anyway:
After you get the room number, then don’t forget to add the number of bottles with the ‘+= operator’, e.g.: