You are not logged in.

#1 2024-10-14 01:08:54

BluePyTheDeer_
Member
Registered: 2024-05-23
Posts: 64

[SOLVED] What is wrong with my Dart code?

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_ (Yesterday 17:38:00)


I messed my Arch Linux installation, then fixed it smile
"Sometimes the best complexity is simplicity." - BluePy, 1856.

Offline

#2 Yesterday 10:01:51

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 22,883

Re: [SOLVED] What is wrong with my Dart code?

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 (Yesterday 10:55:41)

Online

#3 Yesterday 17:23:30

BluePyTheDeer_
Member
Registered: 2024-05-23
Posts: 64

Re: [SOLVED] What is wrong with my Dart code?

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_ (Yesterday 17:26:01)


I messed my Arch Linux installation, then fixed it smile
"Sometimes the best complexity is simplicity." - BluePy, 1856.

Offline

Board footer

Powered by FluxBB