-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossTotal.java
36 lines (33 loc) · 1.19 KB
/
CrossTotal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package de.niklas.exercise.strings;
import java.util.Scanner;
/**
* <strong>Quersumme</strong><br>
* Einlesen einer Zahl und Ausgabe der Quersumme
*
* @see "10_Strings_Aufgaben.pdf"
* @author Niklas Buse
*/
public class CrossTotal {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Bitte Zahl für Quersumme eingeben: ");
String eingabe = scan.next();
int ergebnis = 0;
for(int i = 0; i < eingabe.length(); i++){
// String zifferAlsString = ""+eingabe.charAt(i); // Hier ist die Lösung mit Wandlung vom char -> String -> Zahl
// ergebnis += Integer.valueOf(zifferAlsString);
ergebnis += eingabe.charAt(i) - '0'; // Hier ist die Lösung mit Wert des Chars - den Wert der ersten Ziffer (0), Rest ist der Ziffernwert
}
System.out.printf("Die Quersumme von %s ist %d.", eingabe, ergebnis);
}
}
/* Beispielausführung
--------------------------------------
Eingabe:
369246
--------------------------------------
Ausgabe:
Bitte Zahl für Quersumme eingeben: 369246
Die Quersumme von 369246 ist 30.
--------------------------------------
*/