2021年5月1日星期六

Subtraction between multiple random input numbers

i want to make user input random number example : 5-3-10-50 , system will split " - " and then the result 5 3 10 50 , how to make subtraction from first number minus second number and so on, like this 5 - 3 = 2 , 2 - 10 = -8 , -8 - 50 = -58 and then system will print the final answer -58

my code :

bool Subtraction = true;  int AskSubtraction = 0;    while (Subtraction)  {      Console.Write("\n" + "input number ( example : 1 - 2 - 3 - 4 ) : ");      var InputNumber = Console.ReadLine();      double Answer = 0;            foreach (var result in InputNumber.Split('-'))          {              if (double.TryParse(result, out _))              {                  double NumberResult = Convert.ToDouble(result);                  Answer -= NumberResult;              }              else              {                  Console.WriteLine("\n" + "Wrong input !");                  AskSubtraction++;              }          }        Console.WriteLine("\n" + "subtraction result : " + Answer);  }  

i know my code is wrong, im beginner i already try to fix this but i cant fix it until now, i hope someone tell me what's wrong with my code and fix it too, thank you.

https://stackoverflow.com/questions/67352091/subtraction-between-multiple-random-input-numbers May 02, 2021 at 08:52AM

没有评论:

发表评论