Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6703949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:55+00:00 2026-05-26T07:13:55+00:00

I want choice == 1 to only be able to be selected five times,

  • 0

I want choice == 1 to only be able to be selected five times, so I initialized a variable firstClass = 0, then set up a do-while for firstClass < 5. I included firstClass++ in my do-while to act as a counter. However, I think firstClass is re-initializing every time I call the method CheckIn(). How can I prevent this from happening? Thanks in advance.

using System;

namespace Assignment7
{
    class Plane
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Airline Reservation System.");
            Console.WriteLine("Where would you like to sit?\n");
            Console.WriteLine("Enter 1 for First Class.");
            Console.WriteLine("Enter 2 for Economy.");
            CheckIn();
        }

        public static void CheckIn()
        {
            int choice = Convert.ToInt32(Console.ReadLine());
            int firstClass = 0;
            int economy = 0;

            if (choice == 1)
            {
                do
                {
                    Console.WriteLine("You have chosen a First Class seat.");
                    firstClass++;
                    CheckIn();
                } while (firstClass < 5);
            }
            else if (choice == 2)
            {
                do
                {
                    Console.WriteLine("You have chosen an Economy seat.");
                    economy++;
                    CheckIn();
                } while (economy < 5);
            }
            else
            {
                Console.WriteLine("That does not compute.");
                CheckIn();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:13:56+00:00Added an answer on May 26, 2026 at 7:13 am

    That is entirely normal. If you want the variable to exist outside the method, you must declare it outside a method, as a “field”. Simply move the:

    int firstClass = 0;
    

    outside the method, adding the static modifier (in this case):

    static int firstClass = 0;
    

    Note also that this by itself is not thread-safe; if threading is an issue (for example, ASP.NET) then use int newValue = Interlocked.Increment(ref firstClass);. I only mention this because in the general case static data should consider threading, but I suspect it isn’t an issue in your case (a console exe).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to reproduce the behavior of the command CHOICE in DOS batch but
I want to use autocomplete (jquery or .net) with force a choice so that
Which version of Linux would be my best choice if I want to use
I have written the following code choice /m Do you want to add another
I want to start a new Intent dynamically. Therefore setClassName seems the best choice.
I am trying to implement a multiple choice quiz and will want to store
want to know why String behaves like value type while using ==. String s1
In my program I want the user to be able to choose between some
I need a better solution for handling unready drives and want to be able
I've been reading StackOverflow for quite a while now and I'm only now building

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.