简体   繁体   中英

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. 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.

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.

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;
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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