✅ Beginner help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_2_Task_1
{
internal class Program
{
static void Main(string[] args)
{
int testscore = 0;
do
{
Console.WriteLine();
Console.Write("Input test score: ");
testscore = Convert.ToInt32(Console.ReadLine());
if (testscore == -1) { Console.Write("Well done, Pres ENTER To Exit...."); Console.ReadLine(); Console.WriteLine(); Environment.Exit(0); } else if (testscore < -1) { Console.WriteLine("Invalid Integer. Please Retry"); } else if (testscore > 100) { Console.WriteLine("Invalid Integer. Please Retry"); }
else if (testscore >= 0 && testscore <= 49) { Console.WriteLine("You need to put in more work!");
} else if (testscore >= 50 && testscore <= 79) { Console.WriteLine("Could you do better? ");
} else if (testscore >= 80 && testscore <= 100) { Console.WriteLine("Well Done!");
}
} while (true);
} } } How can i make it so if they enter like a word 'dog' it says invalid. And Re loops again
if (testscore == -1) { Console.Write("Well done, Pres ENTER To Exit...."); Console.ReadLine(); Console.WriteLine(); Environment.Exit(0); } else if (testscore < -1) { Console.WriteLine("Invalid Integer. Please Retry"); } else if (testscore > 100) { Console.WriteLine("Invalid Integer. Please Retry"); }
else if (testscore >= 0 && testscore <= 49) { Console.WriteLine("You need to put in more work!");
} else if (testscore >= 50 && testscore <= 79) { Console.WriteLine("Could you do better? ");
} else if (testscore >= 80 && testscore <= 100) { Console.WriteLine("Well Done!");
}
} while (true);
} } } How can i make it so if they enter like a word 'dog' it says invalid. And Re loops again
22 Replies
i mean, if the user enters anything but an interger, it just says invalid, Please Enter a valid integer and then displays the Input test score again
$tryparse
When you don't know if a string is actually a number when handling user input, use
int.TryParse
(or variants, e.g. double.TryParse
)
TryParse
returns a bool
, where true
indicates successful parsing.
Remarks:
- Avoid int.Parse
if you do not know if the value parsed is definitely a number.
- Avoid Convert.ToInt32
entirely, this is an older method and Parse
should be preferred where you know the string can be parsed.
Read more hereis there anyway to do it with the else, else if or if loops?
i mean
without tryparse
When pasting $code, its really helpful if you do it right so we get syntax highlighting and a monospace font.
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/i havent learnt that yet
okay
Well you can parse it manually but...
TryParse
will be way easier.TryParse
is 100% the way to go here. Your alternatives are manual parsing (much harder) or try/catch (more complicated)well you could iterate through each character in the string and check if its a digital number or not
It is interesting to learn the pattern try parses uses though.
i know but if they see ive used something we havent covered in class that wont go well for me
we have covered try catch
then use try catch
^
but be aware that its the bad way to do this, in the future
yeah im noty sure why he hasnt taught us tryparse
Since you stipulated it should loop, you can also use
continue;
in future to skip the current loop & move onto the nextokay thankyou guys
this is like the 'standard' int parsing pattern.
okay i see
thanks
if they taught them int.Parse i dont see why we should use this
or Convert.ToInt32
🤷 I don't either, but they asked for a method using loops.
this is a method using loops.