好吧,我有问题。我的代码打印出文本,提供了输入三种内容的选项。如果您键入其中一个,它会写一行。但无论出于什么原因,它只是在打印出文本后立即关闭(在visual studio中我会使用Ctrl + f5),但因为我试图从.exe启动而无法做到这一点。在打印结果后,如何使窗口保持打开而不关闭。
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
/// <summary>
/// This console application was written by Graham Long.
///
/// This is a series of basic programming items that I am using to help myself learn c#
/// </summary>
class Program
{
//Below is a function
static void Main(string[] args)
{
//All of the code starts here. Some call this an 'entry point'.
String myString = "Hello world! My name is Graham and I am the developer of this application.";
Console.WriteLine(myString);
Console.WriteLine(" ");
Console.WriteLine("Type 'q' for an explanation of some British slang.");
Console.WriteLine("Type 'w' for some meta text.");
Console.WriteLine("Type 'e' for the same compliment printed twice.");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if(keyInfo.KeyChar == 'q')
{
Console.WriteLine("'Chav' is British slang for 'Council House And Violent', council house meaning a house on rent from the council (They're cheap, which means they're usually associated with poorness) and violent meaning easily angered and vicious.");
}
else if(keyInfo.KeyChar == 'w')
{
printSomeTextToScreen();
}
else if(keyInfo.KeyChar == 'e')
{
printNiceTextToScreenTwice();
}
//Console.WriteLine("Did you type {0}?", keyInfo.KeyChar.ToString());
}
static void printSomeTextToScreen()
{
//Using a separate function from 'Main' ('Main' is the main function).
String someText = "This is some text being printed from a different function.";
Console.WriteLine(someText);
}
static void printNiceTextToScreenTwice()
{
//Using another separate function from 'Main', this function includes a 'for' loop which is a piece of code that runs until a condition is met.
String niceText = "You have a lovely fore arm.";
for (uint counter = 0; counter < 2; counter++)
{
Console.WriteLine(niceText);
}
}
}
}
抱歉提前格式错误(以防万一)。
答案 0 :(得分:1)
如果您没有告诉应用程序执行任何操作,那么它就会完成。例如,您可以在Main()
方法的末尾添加此行:
Console.ReadKey();
这告诉程序要等到另一个键被击中。
答案 1 :(得分:0)
使用while循环,根据输入的某些键,您可以退出循环。
static void Main(string[] args)
{
//All of the code starts here. Some call this an 'entry point'.
do
{
String myString = "Hello world! My name is Graham and I am the developer of this application.";
Console.WriteLine(myString);
Console.WriteLine(" ");
Console.WriteLine("Type 'q' for an explanation of some British slang.");
Console.WriteLine("Type 'w' for some meta text.");
Console.WriteLine("Type 'e' for the same compliment printed twice.");
Console.WriteLine("Type 's' to close.");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.KeyChar == 'q')
{
Console.WriteLine("'Chav' is British slang for 'Council House And Violent', council house meaning a house on rent from the council (They're cheap, which means they're usually associated with poorness) and violent meaning easily angered and vicious.");
}
else if (keyInfo.KeyChar == 'w')
{
printSomeTextToScreen();
}
else if (keyInfo.KeyChar == 'e')
{
printNiceTextToScreenTwice();
}
else if (keyInfo.KeyChar == 's')
{
break;
}
}
while (true);
}