0
Error on code
Hello! I am fairly new to coding and I am currently learning c#. I was practicing what I have learned, but it says that this is wrong. This is the output error message: Syntax error, ā(ā expected ) expected āelse cannot start a statement Syntax error, ā(ā expected Invalid expression term āelseā ) expected ; expected ; expected This is my code. https://code.sololearn.com/chtS89uj1I49/?ref=app What do I need to fix?
2 Answers
+ 2
Hello Coder09
If statements should be in this form:
if(condition){
}
You are asking for a string, so you should compare the answer with "19"
if(answer == "19"){
...
And you are missing ; after Console.WriteLine("Wrong")
Here is the correct code:
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)
{
string answer;
Console.WriteLine("What is 9+10");
answer = Console.ReadLine();
if(answer == "19")
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong!");
}
}
}
}
+ 1
// or
if (Convert.ToInt32(answer) == 19)