简体   繁体   中英

C# Console Application - Calling Main method or Class?

I have exercise/drill/homework assignment to create a C# program to compute the value of a number raised to the power of a second number. Read the two numbers from the keyboard.

Ask the user for an integer.
Print the integer back to the screen and asked if it is correct.
If the integer is correct, continue on.
If the integer is incorrect, start program from the beginning.

I have two questions: Can and how do I programmatically clear the console window?

Start over, do I call the Main method or the Class? How do I do either, calling the main method or the class?

Here's what I've written so far:

using System;
using System.Text;

namespace CalcPowerOfNums
{
    class Program
    {
        //Declaring the two main string variables to be used in our calculation.
        string firstUserString;
        string secondUserString;      

        static void Main(string[] args)
        {
            //Ask the user for the first number.
            Console.WriteLine("Enter your first number and press the Enter/Return key");
            string firstUserString = Console.ReadLine();

            //Make sure this number is correct.
            Console.WriteLine("You want to find the power of {0}?\n" , firstUserString);

            //Declaring, Initializing string variables for user answer.
            string firstAnswer = "";

            //Make user confirm or deny their choice.
            Console.WriteLine("Press the lowercase letter y for yes");
            Console.WriteLine("Press the lowercase letter n for no");
            Console.ReadKey();

            //If user answer is yes, move on… It user answer is no, start program over.
            do
            {
                if (firstAnswer == "y")
                    continue;
                if (firstAnswer == "n")

            }

Looking at the Console class, you will find a Clear method that will clear the console screen. As to calling Main, that will be called automatically by default in a console project as long as you have declared it. You can have a look in your project properties at the Startup Object setting.

When you say "start the program over" I am assuming you mean clear the window and ask for the input again, not reload the entire process.

You can use Console.Clear() to clear the console window. The main method is called automatically from Program.cs. Just place your main code in a while loop and loop until you get the desired input. If you don't get the desired input, just issue a Console.Clear() and ask again until you do.

I have two questions: Can and how do I programmatically clear the console window?

Yes, by calling Console.Clear .

Do I call the Main method or the Class?

You cannot invoke a class, and you should never call main directly. Just put a do/while loop around it with 'is correct' as the condition:

do {
    ...all regular code...
} while(firstAnswer == 'y');

What about:

Console.Clear();

?

you should make functions for this. Put the enter name part in a function then call this function at the start of the Main function and in the if statements.

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