-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddUp.java
59 lines (55 loc) · 1.44 KB
/
AddUp.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package de.niklas.exercise.control;
/**
* <strong>Einmaleins</strong><br>
* Addieren beliebig vieler Eingaben (größer 0)
* Einmal als While Schleife, auskommentiert die Lösung mit do-while
*
* @see "06_Kontrollstrukturen_Aufgaben.pdf"
* @author Niklas Buse
*/
public class AddUp {
public static void main(String[] args) {
java.util.Scanner scan = new java.util.Scanner(System.in);
int ergebnis = 0;
while(true){
System.out.print("Zahl eingeben (<0 für Abbruch): ");
int zahl = scan.nextInt();
if(zahl < 0){
System.out.printf("Summe: %d", ergebnis);
break;
}
else{
ergebnis += zahl;
}
}
// do{
// System.out.print("Zahl eingeben (<0 für Abbruch): ");
// int zahl = scan.nextInt();
// if(zahl < 0){
// System.out.printf("Summe: %d", ergebnis);
// break;
// }
// else{
// ergebnis += zahl;
// }
// }while (true);
}
}
/* Beispielausführung
--------------------------------------
Eingabe:
1
2
3
4
-1
--------------------------------------
Ausgabe:
Zahl eingeben (<0 für Abbruch): 1
Zahl eingeben (<0 für Abbruch): 2
Zahl eingeben (<0 für Abbruch): 3
Zahl eingeben (<0 für Abbruch): 4
Zahl eingeben (<0 für Abbruch): -1
Summe: 10
--------------------------------------
*/