C
C#2w ago
Moon

✅ need help with a basic code

nt x= 6 ; int y = 7; Console.WriteLine("what is x+y?"); string input = Console.ReadLine(); int answer; if (int.TryParse(input, out answer)) { if (answer == x+y) { Console.WriteLine("correct"); { else } Console.WriteLine("wrong, right answer is:)" + (x+y)); { else } Console.WriteLine("enter a valid num"); Console.ReadLine();
24 Replies
Moon
MoonOP2w ago
what is the mistake here
Pobiega
Pobiega2w ago
Would you mind fixing the formatting first? $code
MODiX
MODiX2w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Moon
MoonOP2w ago
d
Pobiega
Pobiega2w ago
And you have some invalid syntax going on here due to bracket placement when formatted correctly, the errors are quite easy to spot
Pobiega
Pobiega2w ago
No description
Moon
MoonOP2w ago
yes i noticed thanks btw can i ask anotehr question
Pobiega
Pobiega2w ago
yes
Moon
MoonOP2w ago
namespace MyApp { internal class Program { static void Main(string[] args) { int x = 6; int y = 7; Console.WriteLine("what is x + y?"); string input = Console.ReadLine(); int answer; if (int.TryParse(input, out answer)) { if (answer == x + y) { Console.WriteLine("correct"); } else { Console.WriteLine("wrong, right answer is: " + (x + y));
} } else { Console.WriteLine("enter a valid number"); string again = Console.ReadLine(); } } } } thats the code
Pobiega
Pobiega2w ago
$codegif
Pobiega
Pobiega2w ago
discord supports code blocks use them
Moon
MoonOP2w ago
i want it to when i answer a wrong number to make it continue like an infinite loop iuntil its right
Pobiega
Pobiega2w ago
sure
Moon
MoonOP2w ago
and it stops on the screen at wrong
Pobiega
Pobiega2w ago
you'd need to use a loop for that
Moon
MoonOP2w ago
what is x + y? 6 wrong, right answer is: 13 look it dosent ask anymore questiions and code should be
Pobiega
Pobiega2w ago
thats because you dont have any loop in your code it wont loop unless you use a loop
Moon
MoonOP2w ago
yeah but i HAVE ELSE AND IF
Pobiega
Pobiega2w ago
sure neither of those are loops while, for and do...whileare loops
Moon
MoonOP2w ago
oh yeah i understood my mistake i didnt read the code good mb , thanks bro
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?