Home c# Creation Console Calculator on C #

Creation Console Calculator on C #

Author

Date

Category

Hello, the essence of the question is – I do not understand why the result of multiplication is 0:

public void multiply ()
{
  Double [] Numbers = New Double [9999999];
  Double Res = 1.0;
  for (int i = 0;; i ++)
  {
    Console.WriteLine ("Please Enter A Number:");
    String A = Console.ReadLine ();
    Double.tryParse (A, Out Numbers [i]);
    res * = numbers [i];
    if (a == "quit")
    {
      Console.WriteLine ($ "Result is {res}");
      Break;
    }
  }
}

I made the same code to add, only in the RES variable I recorded 0.0, but it worked correctly.
Please explain what error is: D


Answer 1, Authority 100%

Because if a is not a number, then in numbers [i] turns out to be zero.

if (double.tryparse (a, out numbers [i]))
    res * = numbers [i];

Answer 2, Authority 100%

Answer from @igor is unconditionally correct, I will only add that if you still remove all the damnings without changing the behavior of the method, the code will turn out:

public void multiply ()
{
  Double Res = 1.0;
  While (True)
  {
    Console.Write ("Please Enter A Number:");
    String A = Console.ReadLine ();
    If (Double.tryParse (A, Out Double Number))
    {
      Res * = Number;
    }
    ELSE if (a == "quit")
    {
      Console.WriteLine ($ "Result is {res}");
      Break;
    }
  }
}

This code will consume about 9999999 times less memory.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions