Eine Summe Schritt für Schritt bilden
Starte bei null und zähle jede Zahl der Reihe nach zur bisherigen Summe dazu.
Ein einfaches Beispiel
Am Anfang2 + 4
Ergebnis6
using System;
int total = 0;
total += 2;
total += 4;
Console.WriteLine(total);
6
Anzeigen
Beispiele aus dem Alltag
Drei Klassen spenden 5, 2 und 4 Bücher. Wie viele Bücher kann die Bibliothek auf ihre Regale stellen?
Am Anfang5 + 2 + 4
Ergebnis11
using System;
int[] books = new int[] { 5, 2, 4 };
int total = 0;
int i = 0;
while (i < 3) {
total += books[i];
i += 1;
}
Console.WriteLine(total);
11
Beispiel ausführen
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson