Home > database >  What am I doing wrong? I'm a beginner and I have no idea
What am I doing wrong? I'm a beginner and I have no idea

Time:10-02

I'm a big beginner and it keeps on saying the it doesn't know what "answer" is. How can I make it recognize it so I can run the code? Ty:)

using System;
 
namespace MyApp 
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Double num01;
            Double num02;
            Double num03;

            Console.WriteLine("Do you want to find the average of three numbers?");

            Console.ReadLine = answer;
            
            if (answer == "Yes")
            {
                Console.Write("Ok, give me a number: ");
           
                num01 = Convert.ToDouble( Console.ReadLine() );
 
                Console.Write("Give me another number: ");
 
                num02 = Convert.ToDouble( Console.ReadLine() );

                Console.Write("And just one more: ");

                num03 = Convert.ToDouble( Console.ReadLine( ));

                Double result = (num01   num02   num03 ) / 3;
            
                Console.WriteLine("The result is: "   result);
           
                Console.ReadKey();
            }
            else
            {
                Console.Write("Oh, ok.");
                Console.ReadKey();
            }
        }
    }
}

CodePudding user response:

I have fixed your code by replace this line string answer = Console.ReadLine();

from

Console.ReadLine = answer;

using System;
 
namespace MyApp 
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Double num01;
            Double num02;
            Double num03;

            Console.WriteLine("Do you want to find the average of three numbers?");

          string answer = Console.ReadLine();
            
            if (answer == "Yes")
            
            {
                Console.Write("Ok, give me a number: ");
           
                num01 = Convert.ToDouble( Console.ReadLine() );
 
                Console.Write("Give me another number: ");
 
                num02 = Convert.ToDouble( Console.ReadLine() );

                Console.Write("And just one more: ");

                num03 = Convert.ToDouble( Console.ReadLine( ));

                Double result = (num01   num02   num03 ) / 3;
            
                Console.WriteLine("The result is: "   result);
           
                Console.ReadKey();
            }
           
            else

            {
                Console.Write("Oh, ok.");

                Console.ReadKey();



            }


    
        }
 
    }

CodePudding user response:

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("Do you want to find the average of three numbers?");
        var answer = Console.ReadLine();

        // compare case-insenstive, both 'Yes' and 'yes' inputs are accepted.
        if (string.Equals(answer,"Yes", StringComparison.CurrentCultureIgnoreCase))
        {
            Console.Write("Ok, give me a number: ");
            var num01 = Convert.ToDouble(Console.ReadLine());

            Console.Write("Give me another number: ");
            var num02 = Convert.ToDouble(Console.ReadLine());

            Console.Write("And just one more: ");
            var num03 = Convert.ToDouble(Console.ReadLine());

            var result = (num01   num02   num03) / 3;
            
            // Show result rounded to 2 decimal places
            Console.WriteLine("The result is: "   Math.Round(result, 2));
            Console.ReadKey();
        }
        else
        {
            Console.Write("Oh, ok.");
            Console.ReadKey();
        }
    }
}
  •  Tags:  
  • c#
  • Related