Home > Blockchain >  C# Unable to input decimal numbers
C# Unable to input decimal numbers

Time:09-15

I'm doing a small task for UNI and I can't find the answer to this problem:

Inputting liner numbers works fine but when trying to insert decimal numbers in the console errors pop up

The code:

using System;

namespace Circle
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Radius R = ");
            String radius = Console.ReadLine();
            Double num1 = 3.1415926;
            Double num2 = Int32.Parse(radius);
            Double RLG = num2 * 2 * num1;
            Double RL = Math.Pow(num2, 2) * num1;
            Double LL = Math.Pow(num2, 2) * 4 * num1;
            Double LT = Math.Pow(num2, 3) * 4 * num1 / 3;

            Console.WriteLine("circle line length "   CLL);
            Console.WriteLine("circle surface size "   CSS);
            Console.WriteLine("sphere surface size "   SSS);
            Console.WriteLine("sphere volume "   SV);
        }
    }
}

CodePudding user response:

So initially there are a few problems with your code.

On this line below, you're parsing 'radius' as an int and saving it to a Double variable. You should use Double.Parse if you want a Double value.

Double num2 = Int32.Parse(radius);

Then in the code below, you're referencing variables which do not exist.

Console.WriteLine("circle line length "   CLL);
Console.WriteLine("circle surface size "   CSS);
Console.WriteLine("sphere surface size "   SSS);
Console.WriteLine("sphere volume "   SV);

If you changed the variables, to the below and Double.Parse'd ...

Console.WriteLine("RLG = "   RLG);
Console.WriteLine("RL = "   RL);
Console.WriteLine("LL = "   LL);
Console.WriteLine("LT = "   LT);

You would get the following output ...

RLG = 165.373434464
RL = 2176.31439754624
LL = 8705.25759018496
LT = 76374.12659122272
  • Related