简体   繁体   English

返回到C#控制台应用程序中的“开始/顶部”

[英]Return to Start/Top in C # console application

I am trying to go back to start in c# console program, and i am somehow able to do it, but the problem is that i am not able to do it using the Y/N statement. 我试图回过头来从C#控制台程序开始,并且我能够做到这一点,但是问题是我无法使用Y / N语句来做到这一点。 mean if i type the Y (yes) from keyboard then the program start from it's starting (beginning) point if i type N then it will terminate the program. 表示如果我从键盘上键入Y(是),则程序从它的开始(开始)点开始,如果我键入N,则它将终止程序。

My Console code is: 我的控制台代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace simple_calculation
{
    class Program
    {
        static void Main(string[] args)
        {
            bool bktop = true;
            string option;
            while (bktop)
            {
            Console.WriteLine("Please enter the values");
            int val = int.Parse(Console.ReadLine());
            int val1 = int.Parse(Console.ReadLine());
            int res = val + val1;
            int red = val / val1;

            Console.WriteLine("Please enter the operator");
            string oper=Console.ReadLine();

                if (oper == "+")
                {
                    Console.WriteLine("sum is:" + res);
                }
                else if (oper == "/")
                {
                    Console.WriteLine("division is:" + red);
                }


            else
            {
                Console.WriteLine("do you want to continue? enter Y/N");
                option = Console.ReadLine();
                    if(option=="Y")
                    {
                        bktop = false;
                    }
                else
                    {
                        bktop = true;
                    }
            }
                Console.ReadLine();
        }  
        }
    }
}

What I want: i want that when the program reaches to the else condition then there is a text appear in the console "do you want to continue? enter Y/N" if the user enter Y then the program start again and if the user enter N then the program will terminate. 我想要的是:我希望当程序达到else条件时,如果用户输入Y,则控制台中会出现一个文本“您要继续吗?请输入Y / N”,然后程序又重新启动,并且用户输入N,程序将终止。

Any help will be appreciated. 任何帮助将不胜感激。

class Program
{
    static void Main(string[] args)
    {
      // Change 1
        bool bktop = true;
        string option;
        while (bktop)
        {
            bktop = true;
            Console.WriteLine("Please enter the values");
            int val = int.Parse(Console.ReadLine());
            int val1 = int.Parse(Console.ReadLine());
            int res = val + val1;
            int red = val / val1;

            Console.WriteLine("Please enter the operator");
            string oper = Console.ReadLine();

            if (oper == "+")
            {
                Console.WriteLine("sum is:" + res);
            }
            else if (oper == "/")
            {
                Console.WriteLine("division is:" + red);
            }


            else
            {

                Console.WriteLine("do you want to continue? enter Y/N");
                option = Console.ReadLine();
                // Change 2
                if (option.ToUpper() != "Y")
                {
                    bktop = false;
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM