0

Why won't the final result show up?

Doing a basic calculator just from my own thought. Can't get the output to come up, any reason? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Console.WriteLine("Enter your first number: "); double num1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(num1); Console.WriteLine("Enter your second number: "); double num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(num2); Console.WriteLine("Would you like to (+), (-), (*), or (/)?: "); string ans = Console.ReadLine(); if (ans == "+") { Console.WriteLine("The answer is ", + (num1 + num2)); } else if (ans == "-") { Console.WriteLine("The answer is ", (num1 - num2)); } else if (ans == "*") { Console.WriteLine("The answer is ", (num1 * num2)); } else { Console.WriteLine("The answer is ", (num1 / num2)); } } } }

7th Dec 2021, 12:13 PM
Ademir
2 Respostas
+ 3
1. String comparison in C# is performed by using Equals() method. So your conditions for checking operations would be if( ans.Equals( "+" ) ) // all other operation goes like this 2. When printing output, use placeholders where you want the value to be Console.WriteLine( "The answer is: {0}", num1 + num2 ); The same format is applicable for other operations. Try to fix these first, then let me know ... P.S. And please put C# into the tags ☝
7th Dec 2021, 12:25 PM
Ipang
+ 1
Thank you for reply. Yes my issue was formatted strings, I did what you mentioned like so... if (ans == "+") { Console.WriteLine("The answer is {0}", (num1 + num2)); } This worked, thank you. Sorry am new, will put tags in from now on!
9th Dec 2021, 3:04 AM
Ademir