Option yes/no C# Console

后端 未结 6 1357
暗喜
暗喜 2021-01-15 01:15

I\'m creating a C# program for the Currency Converter by console. At the end I would to insert \"Continue? (yes/no)\". Here the user have to chose. I\'ve tried this but it

相关标签:
6条回答
  • you want something like that

    float Dollaro = 1.32f, Euro;
    float Cambio;
    string EuroStr;
    
         do
         {
            Console.Write("Euro: ");
            EuroStr = Console.ReadLine();
            Euro = float.Parse(EuroStr);
    
            Cambio = Dollaro * Euro;
    
            Console.WriteLine("Dollaro: " + Cambio);
            Console.WriteLine("Vuoi continuare? (yes/no)");
            Console.ReadLine();
            string risposta = Console.ReadLine();
    
                if (risposta.Equals("Y"))
                {
                    Console.WriteLine("Yes");
                    break;
                }
                else if (risposta.Equals("N"))
                {
                    Console.WriteLine("No");
                    break;
                }
    
          } while (risposta == "N");
    

    but that's just a sample, you need to give more information so i'll give you better example. what does your code supposed to do? what other option does the user have? and so on

    0 讨论(0)
  • 2021-01-15 01:22

    You need to 'read' answer to be able to test it.

    answer = Console.ReadLine();
    
    0 讨论(0)
  • 2021-01-15 01:34

    You should move code where you do operation to do while loop.

    Try this:

        static void Main(string[] args)
        {
            float Dollaro = 1.32f, Euro;
            float Cambio;
            string EuroStr;
    
            string risposta = "Y";
    
            do
            {
                Console.Write("Euro: ");
                EuroStr = Console.ReadLine();
                Euro = float.Parse(EuroStr);
    
                Cambio = Dollaro * Euro;
    
                Console.WriteLine("Dollaro: " + Cambio);
                Console.WriteLine("Vuoi continuare? (yes/no)");               
                risposta = Console.ReadLine();
    
                if (risposta.Equals("Y"))
                {
                    Console.WriteLine("Yes");                    
                }
                else if (risposta.Equals("N"))
                {
                    Console.WriteLine("No");                    
                }
    
            } while (risposta == "Y");
        }
    
    0 讨论(0)
  • 2021-01-15 01:37

    This should fix your problem: And you should make your variable "risposta" ToLower so that it doesnt matter if you type a small or big letter (y or Y)

    float Dollaro = 1.32f, Euro;
                float Cambio;
                string EuroStr;
    
                string risposta = "Y";
    
                do
                {
                    Console.Write("Euro: ");
                    EuroStr = Console.ReadLine();
                    Euro = float.Parse(EuroStr);
    
                    Cambio = Dollaro * Euro;
    
                    Console.WriteLine("Dollaro: " + Cambio);
                    Console.WriteLine("Vuoi continuare? (yes/no)");
                    risposta = Console.ReadLine();
    
                    if (risposta.Equals("Y"))
                    {
                        Console.WriteLine("Yes");
                    }
                    else if (risposta.Equals("N"))
                    {
                        Console.WriteLine("No");
                    }
    
                } while (risposta.ToLower() == "y");
    
    0 讨论(0)
  • 2021-01-15 01:40

    Try this code:

    int num1, num2; 
    char oPt;
    string Count;
    do
    {
        Console.WriteLine("Enter 1st Value");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter 2nd Value : ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(" + - * / ");
        oPt = Convert.ToChar(Console.ReadLine());
    
        if (oPt == '-')
        {
            Console.WriteLine("Result: " + (num1 - num2));
        }
        else if (oPt == '+')
        {
            Console.WriteLine("Result: " + (num1 + num2));
        }
        else if (oPt == '*')
        {
            Console.WriteLine("Result: " + (num1 * num2));
        }
        else if (oPt == '/')
        {
            Console.WriteLine("Result: " + (num1 / num2));
        }
        do
        {
            Console.WriteLine("Do you wish to calculate another? Yes (y) or No (n): ");
            Count = Console.ReadLine();
            var CountLower = Count?.ToLower();
            if ((CountLower == "y") || (CountLower == "n"))
                break;
            } while (true );
        } while (Count != "n");
    } 
    
    0 讨论(0)
  • 2021-01-15 01:42
            float Dollaro = 1.32f, Euro, Cambio;
            string EuroStr;
            ConsoleKeyInfo risposta;
            do
            {
                Console.Write ( "Euro: " );
                EuroStr = Console.ReadLine ();
                bool result = Single.TryParse ( EuroStr, out Euro );
                if ( result )
                {
                    Cambio = Dollaro * Euro;
                    Console.WriteLine ( "Dollaro: " + Cambio );
                } else {
                    Console.WriteLine ( "Please enter a number" );
                }
                bool check = false;
                do {
                    Console.Write ( "\nVuoi continuare? (yes/no) " );
                    risposta = Console.ReadKey ( true );
                    check = !( ( risposta.Key == ConsoleKey.Y ) || ( risposta.Key == ConsoleKey.N ) );
                } while ( check );
                switch ( risposta.Key )
                {
                    case ConsoleKey.Y: Console.WriteLine ( "Yes" ); break;
                    case ConsoleKey.N: Console.WriteLine ( "No" ); break;
                } 
            } while ( risposta.Key != ConsoleKey.N );
    

    I've changed some things:

    • if I enter a character for the Euro - FormatException msdn. So I've added a TryParse();
    • I've changed the response from string to ConsoleKeyInfo msdn - this makes the check for "Y" or "N" easier and I think clearer, and there is no need to cast the user input with ToLower() msdn and compare it with a string;
    • also a check if the user presses "Y" or "N", while the input is different, the same message will appear - Console.Write ( "\nVuoi continuare? (yes/no) " );

    In general you should filter all data\info ( whatever ) comes from the user, to avoid exception.

    0 讨论(0)
提交回复
热议问题