[英]C# Console Application - Calling Main method or Class?
我进行了练习/练习/作业,以创建一个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")
}
查看Console类,您将找到一个Clear方法,它将清除控制台屏幕。 至于调用Main,只要您声明了它,它将默认在控制台项目中自动调用。 您可以在“启动对象”设置中查看项目属性。
当您说“重新启动程序”时,我假设您的意思是清除窗口并再次请求输入,而不是重新加载整个过程。
您可以使用Console.Clear()
清除控制台窗口。 从Program.cs自动调用main方法。 只需将您的主代码放在while循环中,直到获得所需的输入。 如果没有得到所需的输入,只需发出Console.Clear()
并再次询问,直到完成。
是的,通过调用Console.Clear
。
您不能调用类,并且永远不要直接调用main
。 只需在其周围放置一个do/while
循环,并以“正确”为条件:
do {
...all regular code...
} while(firstAnswer == 'y');
关于什么:
Console.Clear();
?
您应该为此做功能。 将输入名称部分放在函数中,然后在Main函数的开始和if语句中调用此函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.