简体   繁体   中英

How do I overload this code to make it accept my entry?

I am developing a program that collects the results of a "bottle drive". The program will prompt the user for a room number, between 1 and 4, and then prompt the user for the number of bottles collected by each room. The user should have the option to type "quit" to display each room and the number of bottles collected.

Here is my code right now, I am having difficulty expanding the program to support multiple rooms.

namespace BottleDrive
{
  class Program
  {
    static void Main(string[] args)
    {
        int roomNumber;
        int numberOfBottles;
        char quit;

        Console.WriteLine("Enter the Room number you are in.");
        string roomSelect = "";
        roomSelect = Console.ReadLine();
        roomSelect = int.Parse(roomSelect);
        if (roomSelect >= 1)
        {
            Console.WriteLine("Enter the number of bottles collected by room one");
            string room1Bottles = "0";
            room1Bottles = Console.ReadLine();
            int room1 = int.Parse(room1Bottles);
            if (room1 == 1)
            {
                room1Bottles += room1;
                Console.WriteLine("Room one has " + room1 + " many bottles collected");
            }
        }
        if (Console.ReadLine() = quit)
        {
            Console.WriteLine("Room one has collected:" + room1Bottles + "\nRoom two has collected:" + room2Bottles + "Press space to quit");
            string systemExit = "";
            if (Console.ReadLine = "")
            {
                systemExit(0);
            }
        }

You could utilize an array (the room number would be used to calculate the index) to keep track of the number of bottles collected:

int[] bottles = new int[4];

while (true)
{
    Console.Write("Enter the room you're in: ");
    string inputString = Console.ReadLine();
    if (inputString == "quit")
        break;
    int room = int.Parse(inputString);

    Console.Write("Bottles collected in room {0}: ", room);
    bottles[room - 1] = int.Parse(Console.ReadLine());
}

for (int i = 0; i < bottles.Length; ++i)
    Console.WriteLine("Bottles collected in room {0} = {1}", i + 1, bottles[i]);

Then you could iterate through the array again once the user types "quit" to show how many bottles were collected.

I doubt your code compiles simply because of this:

string roomSelect = "";
roomSelect = Console.ReadLine();
roomSelect = int.Parse(roomSelect);

Use an integer variable to store the parsed room number.

Because you only need to know one variable per room you can use a array of integers, in this case an array with 4 entries. You will get input from user and convert to int. You can then use the number to access the correct array index.

  int[] arr_rooms = new int[4];
 Console.WriteLine("Enter room number");
 int number = Int.Parse(Console.ReadLine());
 Console.WriteLine("enter number of bottles");
 int bottles = Int.Parse(Console.ReadLine());

 arr_rooms[number - 1] = bottles; //the -1 is because arrays start at 0

 Console.WriteLine("bottles in room 1 is " + arr_rooms[0].toString());

You would have to declare an array to store the number of bottles:

static void Main(string[] args)
{
    const int NumberOfRooms = 4;

    int[] numberOfBottles = new int[NumberOfRooms];

    while (true) {
        Console.WriteLine("Enter the Room number you are in or 'q' and <enter> to quit.");
        string line = Console.ReadLine();
        if (line.ToLower().StartsWith("q")) {
            break;
        }
        int roomNumber = int.Parse(line) - 1;
        if (roomNumber >= 0 && roomNumber < NumberOfRooms) {
            Console.WriteLine("Enter the number of bottles collected by room number " + (roomNumber + 1));
            line = Console.ReadLine();
            int bottles = int.Parse(line);
            numberOfBottles[roomNumber] += bottles;
            Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[roomNumber], roomNumber + 1);
            Console.WriteLine();
        }
    }
    Console.WriteLine();
    for (int i = 0; i < NumberOfRooms; i++) {
        Console.WriteLine("{0} bottles collected for room number {1}", numberOfBottles[i], i + 1);
    }
    Console.WriteLine("Press any key to quit.");
    Console.ReadKey();
}

Note also that arrays are indexd from 0 to number of items minus one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM