My program runs but if you compile it closes upon input being placed
Here's the code Note:I couldn't understand the answers to the other questions Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Input_Program
{
class Program
{
static void Main()
{
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if (Y == 'Y')
{
Console.WriteLine("Thank you,Please w开发者_如何学Goait.....");
Console.WriteLine("You input Y");
}
}
}
}
If(You can compile it id appreciate it!)
Simply adding a single line to you existing code will make it wait for another keystroke prior to exiting, this will allow you to see your output at best:
if(Y == 'Y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
Console.ReadLine();
An alternative is to run your code within a loop which checks for Q
, or exit
, or something similar as input, and exit the loop and therefore quitting the application as desired - otherwise just keep processing input.
For instance, you could rearrange your code to look something like this:
Console.WriteLine("Input a NON capital y or n.");
char input;
while((input = Console.ReadKey().KeyChar) != 'n')
{
if(input == 'y')
{
Console.WriteLine("You entered y");
}
}
Console.WriteLine("You entered 'n'");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
Another answer suggests running through the command-line being an option; while this is possible it would quickly become tedious to have to execute anything outside of your IDE or even anything extra within your IDE if unnecessary - however, you could look into using this practice for deployed console applications, if it never needs to process more than one command or can handle batch commands.
You have 2 options
- Running the exe from a command prompt.
- Add Console.Readline() as the last line of the Main() function. This will cause your program to wait on the last line of the method until you hit enter.
You're getting the key before you ever prompt for it.
You ask for a NON-capital (lowercase) 'y' or 'n', but compare to a CAPITAL (uppercase) 'Y'.
You're not waiting to see the output before the program exits. Add:
Console.ReadLine();
at the end of Main(), as Naraen said.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Input_Program {
class Program {
static void Main() {
char Y = Console.ReadKey().KeyChar;
Console.WriteLine("Welcome to my bool program!");
Console.WriteLine("Input a NON capital y or n when told to.");
if(Y == 'Y')
{
Console.WriteLine("Thank you,Please wait.....");
Console.WriteLine("You input Y");
}
Console.Readline(); //This will wait for you tu press enter before finishing the program :P
}
}}
Programs that doesn't have graphic interface are closed when the main function is finished, so in this case you don't have time to see what is printed. You could also run it in the command prompt.
精彩评论