简体   繁体   English

C#控制台应用程序-调用Main方法或类?

[英]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. 我进行了练习/练习/作业,以创建一个C#程序来计算一个数字的值,该数字被提升为第二个数字的幂。 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? 重新开始,我该调用Main方法还是Class? How do I do either, calling the main method or the class? 我该怎么做,调用main方法或类?

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. 查看Console类,您将找到一个Clear方法,它将清除控制台屏幕。 As to calling Main, that will be called automatically by default in a console project as long as you have declared it. 至于调用Main,只要您声明了它,它将默认在控制台项目中自动调用。 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. 您可以使用Console.Clear()清除控制台窗口。 The main method is called automatically from Program.cs. 从Program.cs自动调用main方法。 Just place your main code in a while loop and loop until you get the desired input. 只需将您的主代码放在while循环中,直到获得所需的输入。 If you don't get the desired input, just issue a Console.Clear() and ask again until you do. 如果没有得到所需的输入,只需发出Console.Clear()并再次询问,直到完成。

I have two questions: Can and how do I programmatically clear the console window? 我有两个问题:是否可以以编程方式清除控制台窗口?

Yes, by calling Console.Clear . 是的,通过调用Console.Clear

Do I call the Main method or the Class? 我该调用Main方法还是Class?

You cannot invoke a class, and you should never call main directly. 您不能调用类,并且永远不要直接调用main Just put a do/while loop around it with 'is correct' as the condition: 只需在其周围放置一个do/while循环,并以“正确”为条件:

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. 将输入名称部分放在函数中,然后在Main函数的开始和if语句中调用此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:从控制台应用程序中调用方法 - C# : Calling a Method out of console application 如何在c#console应用程序中从Static void main调用其他类的方法? - How to call a method of other class from Static void main in c# console application? 将C#控制台应用程序转换为服务(主要方法不起作用) - Converting C# Console Application to a Service (Main method not working) C#从主类调用一个带参数的方法 - C# Calling a method from the main class with parameters C#通过其他类调用主窗体方法 - C# Calling a main form method via other class C#:如何通过在主方法中调用 ToString 方法在控制台中打印出来 - C#: How to have a ToString method be printed out in the console by calling it in the main method C#console应用程序:主方法返回值VS Application.ExitCode - C# console application: Main method return value VS Application.ExitCode 在C#类中调用方法 - Calling a method in a C# class OOP:使用C#在main方法中实例化一个包含控制台方法的类? - OOP: instantiating a class containing a console method within a main method using C#? 在VS c#console应用程序中更改源文件以编译项目中的Main()方法 - Changing a source file to compile Main() method in a project, in VS c# console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM