Reputation: 51
I am just starting to learn C# as my first programming language, so I decided to make a simple question and answer C# console application. The console will ask you a question, and if you get it right it will say "Correct". After a while I got it all to work with no errors, except it never said "Correct"
using System;
namespace Project
{
class MainClass
{
public static void Main (string[] args)
{
byte answer = 0;
Console.WriteLine ("What is 27+4?");
Console.ReadLine ();
if (answer == 31)
{
bool answerCorrect = true;
if (answerCorrect == true)
Console.WriteLine ("Correct!");
}
}
}
}
Thanks for all your help!
Upvotes: 0
Views: 1544
Reputation: 301
First I would use int
instead of byte
. Then you have to assign the userinput to your variable answer
. The type int
provides a method called .Parse(string s)
which tries to convert the string you get from Console.ReadLine()
to an int. Of course it fails if the input is something like "hello"
. That's a good thing to look at to improve later.
Your use of bool answerCorrect
may be correct, but remember: the comparison always "returns" a bool, so you don't really need an extra one.
Lastly, you're missing one important line Console.Read();
at the very end in your main
-method. It's a bit cheaty but your program then waits for a userinput and the console window stays open and you can actually see what's in there.
static void Main(string[] args)
{
int answer = 0;
Console.WriteLine("What is 27 + 4?");
answer = int.Parse(Console.ReadLine());
if (answer == 31)
{
Console.WriteLine("Correct!");
}
else //I added this part for beauty reasons
{
Console.WriteLine("Incorrect!");
}
Console.Read();
}
I recommend you to have a look at while
to run your program as long as the user gives the wrong answer and try..catch
to accept "hello"
as an input but handle it differently, to improve even more.
Good job though for your first C# application.
Upvotes: 0
Reputation: 185
Console.Readline() returns the string value entered by the user, so firstly capture that input.
string input = Console.ReadLine ();
Next check if the input is correct by using String.Equals method to compare
if(input.Equals("31"))
and lastly there's no need to create and assign a value to an answerCorrect
variable since if the code goes into the if
statement the answer is correct, so just Console.WriteLine ("Correct!")
inside.
Upvotes: 0
Reputation: 354
Try this one.
using System;
namespace Project
{
class MainClass
{
public static void Main (string[] args)
{
byte answer = 0;
Console.WriteLine ("What is 27+4?");
answer = byte.Parse(Console.ReadLine ());
if (answer == 31)
{
bool answerCorrect = true;
if (answerCorrect == true)
Console.WriteLine ("Correct!");
}
}
}
}
Upvotes: 0
Reputation: 9763
You must store the string return value from ReadLine and parse that. Like so:
byte answer = 0;
Console.WriteLine("What is 27+4?");
string s = Console.ReadLine();
if (byte.TryParse(s, out answer) && answer == 31)
{
bool answerCorrect = true;
if (answerCorrect == true)
Console.WriteLine("Correct!");
}
Upvotes: 0
Reputation: 43523
Because Console.ReadLine()
reads your input(as a string), but it's not assigned to your variable answer
. To make your program work, change the line to:
answer = byte.Parse(Console.ReadLine());
But remember to improve your code, for example, using int
instead of byte
, and the code in if (answer == 31)
block can be shorter, etc. Good luck.
Upvotes: 2