C#控制台应用程序 - 调用Main方法还是Class?

时间:2013-05-10 14:30:43

标签: c# main-method

我有练习/训练/家庭作业分配来创建一个C#程序来计算一个数字值增加到第二个数字的幂。从键盘上读取两个数字。

询问用户是否有整数 将整数打印回屏幕并询问是否正确 如果整数正确,请继续。
如果整数不正确,请从头开始编程。

我有两个问题: 可以以及如何以编程方式清除控制台窗口?

重新开始,我可以调用Main方法还是Class? 如何调用main方法或类?

这是我到目前为止所写的内容:

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")

            }

5 个答案:

答案 0 :(得分:0)

查看Console类,您将找到一个清除控制台屏幕的Clear方法。至于调用Main,只要你声明了它,它将在控制台项目中默认自动调用。您可以在“启动对象”设置中查看项目属性。

答案 1 :(得分:0)

当你说'#34;启动程序"我假设您的意思是清除窗口并再次请求输入,而不是重新加载整个过程。

您可以使用Console.Clear()清除控制台窗口。从Program.cs自动调用main方法。只需将主代码置于while循环中并循环,直到获得所需的输入。如果您没有获得所需的输入,只需发出Console.Clear()并再次询问,直到您这样做。

答案 2 :(得分:0)

我有两个问题:可以以及如何以编程方式清除控制台窗口?

是的,致电Console.Clear

我可以调用Main方法还是Class?

您无法调用类,也不应直接调用main。只需在其周围放置do/while循环,并将'is correct'作为条件:

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

答案 3 :(得分:0)

怎么样:

Console.Clear();

答案 4 :(得分:0)

你应该为此做功能。将输入名称部分放在函数中,然后在Main函数的开头和if语句中调用此函数。

相关问题