使用以下代码发现它很困难

时间:2014-07-26 22:32:35

标签: c#

我已经写到这个地方而且我被困了,我需要有关如何终止程序或继续的帮助。

我的意思是,当我提出问题时,您是否希望今天退出,如果他们的答复为否则该程序应该终止,但如果是,则应该继续。

我错过了什么? 请执行程序应该使用N for NO语句终止的方面我没有收到答案。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int amount = 20000;
            int choice, pin = 0, x = 0;
            Console.WriteLine("Enter your pin");
            pin = int.Parse(Console.ReadLine());
            Console.WriteLine("welcome to HSPUIC bank would you like to make a withdraw today N or Y");
            Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。您缺少的是获取和评估用户输入 - 这是Console.ReadLine method (as mentioned in the comments)返回的信息,如下所示:

line = Console.ReadLine();

您的代码可能如下所示:

int amount = 20000;
int choice, pin = 0, x = 0;
Console.WriteLine("Enter your pin");
pin = int.Parse(Console.ReadLine());
Console.WriteLine("welcome to HSPUIC bank would you like to make a withdraw today N or Y");
// save user input
var userInput = Console.ReadLine();
// evaluate if user wants to continue or not
if (userInput.ToLower() == "y")
{
    // if yes, go further
    Console.WriteLine("continue with other action...");
}
// else bye
Console.WriteLine("goodbye");

PIN的行已经使用了用户输入!问题也可以这样做。如果你想留在循环中,直到用户不想再撤回,那么你需要的不仅仅是if-else。请查看dowhile以及// user input = y or n string choice; // user pin int pin = 0; // state that indicates if the user wants to continue or not bool continueLoop = false; do { // greet user Console.WriteLine("welcome to HSPUIC bank would you like to make a withdraw today N or Y"); // take input choice = Console.ReadLine(); // check if user has entered valid input if (choice.ToLower() == "y" || choice.ToLower() == "n") { // default decision is "user does not want to continue" = exit continueLoop = false; // user has choosen to continue if (choice.ToLower() == "y") { // user wants to do something, so stay in the loop continueLoop = true; // ask for pin Console.WriteLine("Enter your pin"); var pinAsText = Console.ReadLine(); // convert the pin to number: if (int.TryParse(pinAsText, out pin)) ... if (pinAsText == "1234") { Console.WriteLine("PIN correct"); // continue with logic here, for example take amount } else { Console.WriteLine("PIN incorrect"); } } } else { Console.WriteLine("Please enter Y or N"); continueLoop = true; } } while (continueLoop); Console.WriteLine("goodbye"); 。{/ p>

解决方案可能如下所示:

welcome to HSPUIC bank would you like to make a withdraw today N or Y
>> Y
Enter your pin
>> 3
PIN incorrect
welcome to HSPUIC bank would you like to make a withdraw today N or Y
>> Y
Enter your pin
>> 1234
PIN correct
welcome to HSPUIC bank would you like to make a withdraw today N or Y
>> N
goodbye

现在流程如下:

{{1}}

答案 1 :(得分:2)

当然,当您的用户有两种不同的选择时,您应该在程序中使用if。您还应该将用户的答案保存到局部变量中以进行处理。

static void Main(string[] args)
    {
        int amount = 20000;
        int choice, pin = 0, x = 0;
        Console.WriteLine("Enter your pin");
        pin = int.Parse(Console.ReadLine());
        Console.WriteLine("welcome to HSPUIC bank would you like to make a withdraw today N or Y");
        char answer = char.Parse(Console.ReadLine());
        if (answer == 'Y')
        {
            //Code that must be executed after choosing "yes" .
            Console.ReadKey();
        }
    }

当你没有为" no" ,你的程序将终止。

此外,您可以使用string代替char

string answer = Console.ReadLine();
        if (answer == "Y")
        {
            //Code that must be executed after choosing "yes" .
            Console.ReadKey();
        }

顺便说一下,你的代码中存在很多可能的错误(例如输入一个字符而不是变量' pin')必须由try-catch处理。