Created
March 10, 2022 08:55
-
-
Save thomasw-mitutoyo-ctl/e7cf8b4b3136d8e34531d672d342a65d to your computer and use it in GitHub Desktop.
Zahlen, die die Ziffer 3 enthalten, gelöst mit Substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
var anzahlZahlenMitDrei = 0; | |
for (var i = 100; i <= 999; i++) { | |
var text = i.toString(); | |
// Nachteil: muss für jede weitere Stelle zusätzlich programmiert werden, z.B. Tausender | |
var hunderter = text.substring(0,1); | |
var zehner = text.substring(1,2); | |
var einer = text.substring(2,3); | |
if (hunderter == "3" || zehner == "3" || einer == "3") { | |
anzahlZahlenMitDrei++; | |
} | |
} | |
print("Anzahl Zahlen mit 3: ${anzahlZahlenMitDrei}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment