You are not logged in.
I have looked for AGES for the solution but it still doesn't do what I tell it to.
Context: It's supposed to be a grade checker I made for fun to experiment with Dart, but it just doesn't work, and when it does, it just decides to print all if statements.
Code:
import 'dart:io';
void main() {
gradeCheck() {
stdout.write("What is your grade, from 0-10?");
String? grade = stdin.readLineSync();
if (grade != null) ; {
int totalGrade = int.parse(grade!);
if (totalGrade == 0) ;
{
print("You failed the test, try harder next time!");
}
if (totalGrade <= 5) ;
{
print("You pass and did well, good job!");
}
if (totalGrade <= 10) ;
{
print("You got a very nice score, great job, keep it up!");
}
if (totalGrade == 10) ;
{
print("And you got a 10, great job!");
}
}
}
gradeCheck();
}
Edit: found the null thing to work by adding a ! next to it, but now it prints all if statements.
Last edited by BluePyTheDeer_ (2024-10-15 17:38:00)
I messed my Arch Linux installation, then fixed it
"Sometimes the best complexity is simplicity." - BluePy, 1856.
Offline
Remove all the semicolons from your if statement lines (which is why you also need to workaround with the nullchecking "!" because you're not letting the if statement determine the scope it needs to run), you're "finishing" the statement that way, which prevents it from actually evaluating the next lines to be ran conditionally based on the result.
Compare with the examples from https://www.darttutorial.org/dart-tutor … f-else-if/ .
Last edited by V1del (2024-10-15 10:55:41)
Offline
Thank you so much! I had tried EVERYTHING from seeing forsaken Stack Overflow forum posts to random pages, just, Thank you!
Finally, I can rest knowing that the Dart script I made finally works as intended.
Last edited by BluePyTheDeer_ (2024-10-15 17:26:01)
I messed my Arch Linux installation, then fixed it
"Sometimes the best complexity is simplicity." - BluePy, 1856.
Offline
I'm fairly certain most resources will have had this correct, but seems you didn't see the forest for the trees unless it was explicitly pointed out
Offline
I had never learnt an OOP language, so I just went "So everything needs ;s, amazing."
I messed my Arch Linux installation, then fixed it
"Sometimes the best complexity is simplicity." - BluePy, 1856.
Offline
OOP has nothing to do with this, you generally need some control characters to know when a statement starts or ends. Assuming you're used to python, where white space/the indent level controls this.
In languages with these charactes you could theorethically write the entire program on a single line, not very practical, but hey
Offline