1 Auftragsentwicklung
2 Schaltjahr
3 Variablen vertauschen
4 Kassensystem erstellen
5 Benzin
6 Benzin2
7 1oder2 (while)
8 Schleife Werte addieren (for und while)
9 Ganzteil
10 Rabatt
11 Rechner (switch)
12 Kleinste Zahl in einem Array finden
13 Array mit Schleife füllen
14 Getränke Automat
15 Lotto (+)
16 Algorithmus analysieren und implementieren (+)
17 Zahlen zwischen 2 einzugebenden Werten ausgeben
18 Kindergeld berechnen
19 1x1 (Matrix)
20 Methode tauschen()
21 Rechteck Methoden()
22 Multiplikation als Addition und Division als Subtraktion()
23 Lebensdauer und Sichtbarkeit
24 Palindrom (+)
25 Array'n Scanner
Last active
April 17, 2018 21:22
-
-
Save d630/c52ee9dbfd8a9e3931730e2d3aa9b3f8 to your computer and use it in GitHub Desktop.
Uebungsaufgaben
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
// TODO |
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
import java.util.Scanner; | |
public class Playground10 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
double value; | |
double rabatt = 0; | |
System.out.print("Einkaufswert: "); | |
value = sc.nextDouble(); | |
if (value >= 5000) { | |
rabatt = value * 0.075; | |
} else if (value >= 1000) { | |
rabatt = value * 0.05; | |
} else if (value >= 100) { | |
rabatt = value * 0.025; | |
} | |
System.out.println("Rabatt: " + rabatt + "\nPreis: " + (value - rabatt)); | |
} | |
} | |
// vim: set ts=2 sw=2 tw=0 et : |
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
import java.util.Scanner; | |
public class Playground11 { | |
public static void main(String[] args) { | |
int i0; | |
int i1; | |
int ergeb = -1; | |
String op = ""; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("zahl_1: "); | |
i0 = sc.nextInt(); | |
System.out.print("zahl_2: "); | |
i1 = sc.nextInt(); | |
System.out.print("operator (+,-,/,*): "); | |
op = sc.next(); | |
switch (op) { | |
case "+": | |
ergeb = i0 + i1; | |
break; | |
case "-": | |
ergeb = i0 - i1; | |
break; | |
case "/": | |
if (i1 != 0) { | |
ergeb = i0 / i1; | |
} else | |
{ | |
System.out.println("Error: DIV/0"); | |
} | |
break; | |
case "*": | |
ergeb = i0 * i1; | |
} | |
System.out.println("Ergebnis: " + ergeb); | |
} | |
} | |
// vim: set ft=java : |
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
public class Playground12 { | |
public static void main(String[] args) { | |
int [] arr = {45,2,8,24,99}; | |
int min = arr[0]; | |
for (int i = 1; i < arr.length; i++) { | |
if (arr[i] < min) { | |
min = arr[i]; | |
} | |
} | |
System.out.println("min := " + min); | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground13 { | |
public static void main(String[] args) { | |
int nm; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Anzahl der Namen? "); | |
nm = sc.nextInt(); | |
String[] array = new String[nm]; | |
for (int i = 0; i < array.length; i++) { | |
System.out.print("Geben Sie den " + (i+1) + ". Namen ein: "); | |
array[i] = sc.next(); | |
} | |
System.out.println("Ihre Eingaben waren: "); | |
for (String e : array) { | |
System.out.println("\t<" + e + ">"); | |
} | |
} | |
} | |
// vim: set ft=java : |
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
// Bei FL bereits gemacht. |
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
// TODO |
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
// TODO |
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
import java.util.Scanner; | |
public class Playground17 { | |
public static void main(String[] args) { | |
int i0; | |
int i1; | |
int start = 0; | |
int end = 0; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("zahl_1: "); | |
i0 = sc.nextInt(); | |
System.out.print("zahl_2: "); | |
i1 = sc.nextInt(); | |
if (i0 > i1) { | |
start = i1; | |
end = i0; | |
} else { | |
start = i0; | |
end = i1; | |
} | |
for (int i = end - 1; i > start; i--) { | |
System.out.print(i + " "); | |
} | |
System.out.println(""); | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground18 { | |
public static void main(String[] args) { | |
double inputEinkommen; | |
double inputKinder; | |
double summe = 0; | |
int moneyCol; | |
Scanner sc = new Scanner(System.in); | |
// unter 45000; über 45000 | |
double[][] money = { | |
{ 70, 70 }, | |
{ 130, 70 }, | |
{ 220, 140 }, | |
{ 240, 140} | |
}; | |
System.out.print("Jahreseinkommen: "); | |
inputEinkommen = sc.nextDouble(); | |
moneyCol = (inputEinkommen < 45000) ? 0 : 1; | |
System.out.print("Kinderanzahl: "); | |
inputKinder = sc.nextDouble(); | |
for (int i = 0; i < inputKinder; i++) { | |
summe += (i >= money.length) ? money[money.length - 1][moneyCol] : money[i][moneyCol]; | |
} | |
System.out.println("Ergebnis: " + summe); | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground19 { | |
public static void printMatrix(int x) { | |
int z = 0; | |
x += 1; | |
for (int i = 1; i < x; i++) { | |
for (int j = 1; j < x; j++) { | |
z++; | |
System.out.print(z + " "); | |
} | |
System.out.println(""); | |
} | |
} | |
public static void printMatrixFormatted(int x) { | |
int z = 0; | |
x += 1; | |
for (int i = 1; i < x; i++) { | |
for (int j = 1; j < x; j++) { | |
z++; | |
System.out.printf("%3d ", z); | |
} | |
System.out.println(""); | |
} | |
} | |
public static void printMatrixFormatted2(int x) { | |
int z = 0; | |
int s; | |
x += 1; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Sie suchen die Ziffer: "); | |
s = sc.nextInt(); | |
if (s == 0) { | |
System.out.println("Error: DIV/0"); | |
return; | |
} | |
for (int i = 1; i < x; i++) { | |
for (int j = 1; j < x; j++) { | |
z++; | |
if (z%s == 0) { | |
System.out.printf("%3s ", "*"); | |
} else { | |
System.out.printf("%3d ", z); | |
} | |
} | |
System.out.println(""); | |
} | |
} | |
public static void main(String[] args) { | |
printMatrix(10); | |
System.out.println(""); | |
printMatrixFormatted(10); | |
System.out.println(""); | |
printMatrixFormatted2(10); | |
} | |
} | |
// vim: set ft=java : |
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
// Bei FL bereits gemacht. |
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
public class Playground20 { | |
public static void tauschen(int a, int b) { | |
System.out.println(b + ", " + a); | |
} | |
public static void main(String[] args) { | |
tauschen(0,1); | |
} | |
} |
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
public class Playground21 { | |
private static void berechneFlaeche(double l, double b) { | |
System.out.println(l*b); | |
} | |
private static double berechneUmfang(double l, double b) { | |
return 2*(l+b); | |
} | |
public static void main(String[] args) { | |
if (args.length < 2) { | |
System.out.println("Error."); | |
System.exit(124); | |
} | |
double l = Double.parseDouble(args[0]); | |
double b = Double.parseDouble(args[1]); | |
berechneFlaeche(l, b); | |
System.out.println(berechneUmfang(l, b)); | |
} | |
} | |
// vim: set ts=2 sw=2 tw=0 et : |
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
public class Playground22 { | |
public static int dividiere(int x, int y) { | |
if (y == 0) { | |
System.out.println("Error: DIV/0"); | |
System.exit(124); | |
} | |
int r = 0; | |
int sign = 1; | |
while (x >= 0) { | |
x -= y; | |
r++; | |
} | |
return (r - 1) * sign; | |
} | |
public static int multipliziere(int x, int y) { | |
int r = 0; | |
int sign = 1; | |
if (x < 0) { | |
x = -x; | |
sign = -sign; | |
} | |
if (y < 0) { | |
y = -y; | |
sign = -sign; | |
} | |
for (int i = 1; i <= x; i++) { | |
r += y; | |
} | |
if (sign < 0) { | |
return -r; | |
} else { | |
return r; | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println(multipliziere(-4, 7)); | |
System.out.println(dividiere(-4, 4)); | |
} | |
} | |
// vim: set ts=2 sw=2 tw=0 et : |
max():
- factor ist sichtbar
- arg ist nicht sichtbar
- n ist nicht sichtbar
- y ist nicht sichtbar
- a ist lokal
- b ist lokal
compute():
- factor ist sichtbar
- arg ist nicht sichtbar
- n ist nicht sichtbar
- x ist lokal
- y ist lokal
main():
- factor ist sichtbar
- arg ist lokal
- n ist lokal
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
public class Playground24 { | |
public static boolean isPalindrom(String str) { | |
StringBuilder sb = new StringBuilder(); | |
for (char e : str.toCharArray()) { | |
sb.insert(0, Character.toString(e)); | |
} | |
return (str.equals(sb.toString())); | |
} | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out.println("Error: arg please"); | |
System.exit(123); | |
} | |
if (isPalindrom(args[0])) { | |
System.out.println("yes"); | |
} else { | |
System.out.println("no"); | |
} | |
} | |
} | |
// vim: set ts=2 sw=2 tw=0 et : |
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
public class Playground25 { | |
public static void main(String[] args) { | |
int nm; | |
String name; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Anzahl der Namen? "); | |
nm = sc.nextInt(); | |
String[] array = new String[nm]; | |
for (int i = 0; i < array.length; i++) { | |
System.out.print("Geben Sie den " + (i+1) + ". Namen ein: "); | |
array[i] = sc.next(); | |
} | |
for (int i = 0; i < array.length; i++) { | |
System.out.print("Wie lautet der " + (i+1) + ". Name?\nName: "); | |
name = sc.next(); | |
if (! name.equals(array[i])){ | |
System.out.println("falsch!\nSie haben verloren"); | |
return; | |
} | |
} | |
} | |
} | |
// vim: set ft=java : |
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
public class Playground3 { | |
public static void main(String[] args) { | |
int i0 = 0; | |
int i1 = 0; | |
int buf; | |
if (args.length >= 2) { | |
i0 = Integer.parseInt(args[0]); | |
i1 = Integer.parseInt(args[1]); | |
}; | |
System.out.println(i0 + " " + i1); | |
buf = i0; | |
i0 = i1; | |
i1 = buf; | |
System.out.println(i0 + " " + i1); | |
} | |
} | |
// vim: set ft=java : |
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
// TODO |
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
public class Playground5 { | |
public static void main(String[] args) { | |
double km = 0.0; | |
double liter = 0.0; | |
Scanner sc = new Scanner(System.in); | |
if (args.length >= 2) { | |
km = Double.parseDouble(args[0]); | |
liter = Double.parseDouble(args[1]); | |
} else { | |
System.out.print("Kilometer: "); | |
km = sc.nextDouble(); | |
System.out.print("Literverbrauch: "); | |
liter = sc.nextDouble(); | |
} | |
System.out.println("Durchschittlicher Literverbrauch auf 100 km: " + liter*100/km); | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground6 { | |
public static void main(String[] args) { | |
double km = 0.0; | |
double liter = 0.0; | |
Scanner sc = new Scanner(System.in); | |
if (args.length >= 2) { | |
km = Double.parseDouble(args[0]); | |
liter = Double.parseDouble(args[1]); | |
} else { | |
System.out.print("Kilometer: "); | |
km = sc.nextDouble(); | |
System.out.print("Literverbrauch: "); | |
liter = sc.nextDouble(); | |
} | |
if (km <= 0) { | |
System.out.println("Error:Kilometeranzahl muss >0 sein."); | |
} else { | |
System.out.println("Durchschittlicher Literverbrauch auf 100 km: " + liter*100/km); | |
} | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground7 { | |
public static void main(String[] args) { | |
int i = 0; | |
int err = 0; | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Zahlenspiel."); | |
while (true) { | |
System.out.print("Wählen sie aus zwischen: \n\ta) 1\n\tb) 2\nEingabe: "); | |
i = sc.nextInt(); | |
System.out.print("Sie haben " + i + " eingegeben."); | |
if (i != 1 && i != 2) { | |
System.out.println(" Nochmal!"); | |
err++; | |
} else { | |
System.out.println(""); | |
break; | |
} | |
} | |
System.out.println("Sie hatten " + err + " Fehlversuche. Bye-bye."); | |
} | |
} | |
// vim: set ft=java : |
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
public class Playground8 { | |
public static void floop () { | |
int summe = 0; | |
for (int i = 1; i < 100; i++) { | |
summe += i; | |
System.out.print(i + " + "); | |
} | |
System.out.println(100 + " = " + (summe + 100)); | |
} | |
public static void wloop() { | |
int summe = 1; | |
String trenn = " + "; | |
StringBuilder ausgabe = new StringBuilder(); | |
ausgabe.append(summe); | |
int i = 2; | |
while (i < 101) { | |
summe += i; | |
ausgabe.append(trenn).append(i); | |
i++; | |
} | |
ausgabe.append(" = " + summe); | |
System.out.println(ausgabe); | |
} | |
public static void main(String[] args) { | |
floop(); | |
wloop(); | |
} | |
} | |
// vim: set ft=java : |
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
import java.util.Scanner; | |
public class Playground9 { | |
public static void main(String[] args) { | |
int i0; | |
int i1; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Gib den Dividenden ein: "); | |
i0 = sc.nextInt(); | |
System.out.println("Sie haben " + i0 + " eingegeben"); | |
System.out.print("Gib den Divisor ein: "); | |
i1 = sc.nextInt(); | |
System.out.println("Sie haben " + i1 + " eingegeben"); | |
System.out.println("Das Ergebnis der Division " + i0 + ":" + i1 + " ist " + i0/i1 + " Rest " + i0%i1); | |
} | |
} | |
// vim: set ft=java : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment