Lektion 2: lösningsförslag
- string s = "abc";
- bool authorized;
- if (s == "secret123") {
- authorized = true;
- }
- else {
- authorized = false;
- }
- string s = "abc";
- bool authorized;
- if (s == "secret123") {
- authorized = true;
- }
- if (s != "secret123") {
- authorized = false;
- }
- int points = 50;
- string grade;
- if (points < 20) {
- grade = "IG";
- }
- else if (points < 40) {
- grade = "G";
- }
- else {
- grade = "VG";
- }
- int points = 50;
- string grade;
- if (points < 0) {
- grade = "ogiltigt";
- }
- else if (points < 20) {
- grade = "IG";
- }
- else if (points < 40) {
- grade = "G";
- }
- else if (points < 60){
- grade = "VG";
- }
- else {
- grade = "fusk!";
- }
- string gender = "man";
- int age = 25;
- bool married = false;
- int children = 3;
- string car = "ABC123";
- string summary = gender + ", " + age;
- if (married)
- {
- summary += ", gift";
- }
- else
- {
- summary += ", ogift";
- }
- if (children > 0)
- {
- summary += ", " + children + " barn";
- }
- if (car != null)
- {
- summary += ", reg " + car;
- }
- double salary = 30000;
- double tax = salary * 0.30;
- int cars = 0;
- int electricCars = 1;
- int flights = 2;
- bool familyAbroad = true;
- string building = "house";
- if (cars == 0)
- {
- tax = tax / 2;
- }
- else if (cars > 1)
- {
- tax += 2000 * (cars - 1);
- }
- if (electricCars >= 1)
- {
- tax -= 1000;
- }
- if (flights >= 2 && !familyAbroad)
- {
- tax = tax * 1.20;
- }
- if (building == "house")
- {
- tax -= 500;
- }
- else
- {
- tax -= 1000;
- }
- if (tax < 0)
- {
- tax = 0;
- }
- (wand && hogwarts) || gandalf
- (wand && hogwarts) || (!wand && gandalf)
- int temperature = 20;
- bool comfortable;
- if (temperature >= 18) {
- if (temperature <= 26) {
- comfortable = true;
- }
- else {
- comfortable = false;
- }
- }
- else {
- comfortable = false;
- }
- string country = "USA";
- int age = 40;
- int elected = 0;
- bool eligible;
- if (country == "USA") {
- if (age >= 35) {
- if (elected <= 1) {
- eligible = true;
- }
- else {
- eligible = false;
- }
- }
- else {
- eligible = false;
- }
- }
- else {
- eligible = false;
- }
- string country = "USA";
- int age = 40;
- int elected = 0;
- bool savedTheWorld = true;
- bool eligible;
- if (country == "USA") {
- if (age >= 35) {
- if (elected <= 1) {
- eligible = true;
- }
- else {
- eligible = false;
- }
- }
- else {
- eligible = false;
- }
- }
- else {
- eligible = false;
- }
- if (savedTheWorld) {
- eligible = true;
- }
- string s = "abc";
- bool authorized = s == "secret123" ? true : false;
- int points = 50;
- string grade = points < 20 ? "IG" : (points < 40 ? "G" : "VG");