Onyeka Franklin
Onyeka Franklin

Reputation: 11

Calculating dart maths using conditional statements

I am trying to build a basic quiz app to improve my, so I was able to use if and else statement to know when they entered the right answer or not then I will print out their score, I was able to achieve this, so the main issue is that I want to add all the sores together and tell them the total they have scored. my code is below. thanks in advance.

import 'dart:io';
import 'dart:math';

void main() {
  print("Hi, welcome to quize app");
  print("what is your name?");
  String? username = stdin.readLineSync();
  print("welcome ${username}");
  print("Who is Christiano ronaldo?");
  List? answers1 = [26, 30, 37, 36];
  List? answers2 = ['Musician', 'Footballer', 'Swimmer'];
  List? answers3 = [3, 5, 7, 17];
  List? answers4 = ['Yes', 'No'];
  int? ans1 = 36;
  String? ans2 = 'Footballer';
  int? ans3 = 17;
  String? ans4 = 'no';
  int? num1 = (0 + 10);
  int? num2 = (0 + 0);
  int? num3 = (10 + 10 + 20);
  print(answers1);
  int? userans1 = int.parse(stdin.readLineSync()!);
  print("Who the next president of Nigeria?");
  print(answers2);
  String? userans2 = stdin.readLineSync();
  print(
      "How many times did national Grid collaps in 2022 alone? please type in numbers:");
  print(answers3);
  int? userans3 = int.parse(stdin.readLineSync()!);
  print('Is Nigerian air still functioning?');
  print(answers4);
  String? userans4 = stdin.readLineSync();
  print('end of quize');
  print('calculating answers...');
  if (userans1 == ans1) {
    String? cal = "$num1";
    print("In question number one (1) you got $cal");
  } else {
    String? cal1 = "$num2";
    print("In question number one (1) you got $cal1");
  }
  if (userans2 == ans2) {
    String? cal = "$num1";
    print("In question Number two (2) you got $cal");
  } else {
    String? cal1 = "$num2";
    print("In question Number two (2) you got $cal1");
  }
  if (userans3 == ans3) {
    String? cal = "$num1";
    print("In question Number three (3) you got $cal");
  } else {
    String? cal1 = "$num2";
    print("In question Number three (3) you got $cal1");
  }
  if (userans4 == ans4) {
    String? cal = "$num1";
    print("In question Number four (4) you got $cal");
  } else {
    String? cal1 = "$num2";
    print("In question Number four (4) you got $cal1");
  }
}

Upvotes: 0

Views: 120

Answers (1)

Moaz El-sawaf
Moaz El-sawaf

Reputation: 3059

It is not just about Dart, it is a general programming logic question.

You can declare a variable of type int to hold the total score and start it with value of 0 like the following:

int totalScore = 0;

then every time the user answers a question correctly, just add the score of the question to the answer, for example:

if (userans1 == ans1) {
    String? cal = "$num1";
    print("In question number one (1) you got $cal");
    totalScore += 10; // 10 is the score user received, replace it with the correct score.
} 

Note: totalScore += 10 is equivalent to totalScore = totalScore + 10

At the end, you will have the Total Score in our variable, do whatever you want with it.

That's it!

Upvotes: 1

Related Questions