update printf/suche/rechner #6

Merged
2210892 merged 1 commits from großerPC into master 2023-04-27 10:47:00 +02:00
3 changed files with 69 additions and 13 deletions

View File

@ -1,18 +1,47 @@
package pr2.auffrischung.printf; package pr2.auffrischung.printf;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DoubleFormatter { public class DoubleFormatter {
public static void printDouble(double d) { public static void printDouble(double d) {
// TODO: Wert ausgeben String format = "0.000";
NumberFormat formatter = new DecimalFormat(format);
String newD = formatter.format(d);
System.out.print("Mit Formatting: " + newD);
System.out.println();
} }
public static void main(String[] args) { public static void main(String[] args) {
printDouble(1.0); double dVal1 = 1.0;
printDouble(10.1); printDouble(dVal1);
printDouble(2.01); System.out.printf("Mit printf: %.3f", dVal1);
printDouble(2.001); System.out.println();
printDouble(2.0001); double dVal2 = 10.1;
printDouble(2.0004); printDouble(dVal2);
printDouble(2.0005); System.out.printf("Mit printf: %.3f", dVal2);
System.out.println();
double dVal3 = 2.01;
printDouble(dVal3);
System.out.printf("Mit printf: %.3f", dVal3);
System.out.println();
double dVal4 = 2.001;
printDouble(dVal4);
System.out.printf("Mit printf: %.3f", dVal4);
System.out.println();
double dVal5 = 2.0001;
printDouble(dVal5);
System.out.printf("Mit printf: %.3f", dVal5);
System.out.println();
double dVal6 = 2.0004;
printDouble(dVal6);
System.out.printf("Mit printf: %.3f", dVal6);
System.out.println();
double dVal7 = 2.0005;
printDouble(dVal7);
System.out.printf("Mit printf: %.3f", dVal7);
System.out.println();
} }
} }

View File

@ -5,8 +5,17 @@ public class GroessteZahl {
public int sucheMax(int[] zahlen) { public int sucheMax(int[] zahlen) {
int max = 0; int max = 0;
// TODO: Methode implementieren for (int i = 0; i < zahlen.length; i++) {
if (max > zahlen[i]) {
max = zahlen[i];
}
}
for (int i = 0; i < zahlen.length; i++) {
if (max < zahlen[i]) {
max = zahlen[i];
}
}
return max; return max;
} }

View File

@ -3,16 +3,34 @@ package pr2.auffrischung.taschenrechner;
public class Taschenrechner { public class Taschenrechner {
public double rechne(double o1, char op, double o2) { public double rechne(double o1, char op, double o2) {
// TODO: Implementieren
switch (op) {
case '+':
return o1 + o2;
case '-':
return o1 - o2;
case '*':
return o1 * o2;
case '/':
if (o2 == 0) {
return o2;
} else {
return o1 / 02;
}
case '^':
return Math.pow(o1, o2);
default:
return 0.0; return 0.0;
} }
}
public static void main(String[] args) { public static void main(String[] args) {
Taschenrechner t = new Taschenrechner(); Taschenrechner t = new Taschenrechner();
System.out.println(t.rechne(1, '+', 2)); System.out.println(t.rechne(1, '+', 2));
System.out.println(t.rechne(1, '-', 2)); System.out.println(t.rechne(1, '-', 2));
System.out.println(t.rechne(2, '*', 2)); System.out.println(t.rechne(2, '*', 2));
System.out.println(t.rechne(4, '/', 2)); System.out.println(t.rechne(4, '/', 2));
System.out.println(t.rechne(2, '^', 32)); System.out.println(t.rechne(2, '^', 3));
} }
} }