Hallo zusammen,
ich hab hier ein paar Fragen an euch die ich mal wieder nicht so ganz verstehe!
Fang ich mal an hier mal 2 Programme die laufen doch leider nicht ganz so wie ich will 😀
Bei diesem Programm bekomme ich ein Rundungsfehler durch den ersten Schritt:
Habt ihr eine Idee wie man das ändern könnte?
6 Cent laufen wieder nur 5 Cent als 2,65 z. B. werden verschlugt 😀
und hier das zweite das läuft komplett nur hab ich eine Frage zu Printf:
Wieso geht es nicht mit %.1g dann nimmt er mir auch immer den Exponent des weiteren bekomme ich das ganze nicht in eine System.out.printf gefasst 🙁 Wieso weis ich nicht so läuft es 😀
Ich hoffe ihr könnt mir ein wenig Helfen.
Für das erste Programm habe ich eine Lösung programmiert die sieht so aus:
Über das Programm hatte ich auch hier schon damals ein paar Fragen doch nun konnte ich es selbst so programmieren =)
LG
ich hab hier ein paar Fragen an euch die ich mal wieder nicht so ganz verstehe!
Fang ich mal an hier mal 2 Programme die laufen doch leider nicht ganz so wie ich will 😀
Java:
public class Muenzen {
public static void main (String [] args) {
double dollar = Double.parseDouble(args[0]);
int buck;
int half;
int quarter;
int dime;
int nickel;
int penny;
int cent = (int) ((dollar - (int) dollar) * 100);
buck = (int) dollar;
half = cent / 50;
cent = cent % 50;
quarter = cent / 25;
cent = cent % 25;
dime = cent / 10;
cent = cent % 10;
nickel = cent / 5;
cent = cent % 5;
//2,65 rundungsfehler!!!
penny = cent;
System.out.println(buck + " x 1 Dollar");
System.out.println(half + " x 50 Cent");
System.out.println(quarter + " x 25 Cent");
System.out.println(dime + " x 10 Cent");
System.out.println(nickel + " x 5 Cent");
System.out.println(penny + " x 1 Cent");
}
}
Bei diesem Programm bekomme ich ein Rundungsfehler durch den ersten Schritt:
Java:
int cent = (int) ((dollar - (int) dollar) * 100);
Habt ihr eine Idee wie man das ändern könnte?
6 Cent laufen wieder nur 5 Cent als 2,65 z. B. werden verschlugt 😀
und hier das zweite das läuft komplett nur hab ich eine Frage zu Printf:
Java:
public class Noten {
public static void main(String[] args) {
double notePoints = Double.parseDouble(args[0]);
System.out.printf( "1.0: %.1f - %.1f (100%% - 95%%) %n",
notePoints, notePoints/100*95);
System.out.printf( "1.3: %.1f - %.1f (94.9%% - 90%%) %n",
notePoints/100*95, notePoints/100*90);
System.out.printf( "1.7: %.1f - %.1f (89.9%% - 85%%) %n",
(notePoints/100*90)-0.01, notePoints/100*85);
System.out.printf( "2.0: %.1f - %.1f (84.9%% - 80%%) %n",
(notePoints/100*85)-0.01, notePoints/100*80);
System.out.printf( "2.3: %.1f - %.1f (79.9%% - 75%%) %n",
(notePoints/100*80)-0.01, notePoints/100*75);
System.out.printf( "2.7: %.1f - %.1f (74.9%% - 70%%) %n",
(notePoints/100*75)-0.01, notePoints/100*70);
System.out.printf( "3.0: %.1f - %.1f (69.9%% - 65%%) %n",
(notePoints/100*70)-0.01, notePoints/100*65);
System.out.printf( "3.3: %.1f - %.1f (64.9%% - 60%%) %n",
(notePoints/100*65)-0.01, notePoints/100*60);
System.out.printf( "3.7: %.1f - %.1f (59.9%% - 55%%) %n",
(notePoints/100*60)-0.01, notePoints/100*55);
System.out.printf( "4.0: %.1f - %.1f (54.9%% - 50%%) %n",
(notePoints/100*55)-0.01, notePoints/100*50);
System.out.printf( "5.0: weniger als %.1f Punkte %n",
(notePoints/100*50)-0.01);
}
}
Wieso geht es nicht mit %.1g dann nimmt er mir auch immer den Exponent des weiteren bekomme ich das ganze nicht in eine System.out.printf gefasst 🙁 Wieso weis ich nicht so läuft es 😀
Ich hoffe ihr könnt mir ein wenig Helfen.
Für das erste Programm habe ich eine Lösung programmiert die sieht so aus:
Java:
public class Muenzen {
public static void main (String [] args) {
double dollar = Double.parseDouble(args[0]);
int buck;
int half;
int quarter;
int dime;
int nickel;
int penny;
buck = (int) dollar;
dollar = dollar - buck;
Math.round(dollar);
half = (int) (dollar / 0.5);
dollar = dollar - half * 0.5;
dollar = dollar * 100;
Math.round(dollar);
dollar = dollar / 100;
quarter = (int) (dollar / 0.25);
dollar = dollar - quarter * 0.25;
dollar = dollar * 100;
Math.round(dollar);
dollar = dollar / 100;
dime = (int) (dollar / 0.10);
dollar = dollar - dime * 0.10;
dollar = dollar * 100;
Math.round(dollar);
dollar = dollar / 100;
nickel = (int) (dollar / 0.05);
dollar = dollar - nickel * 0.05;
dollar = dollar * 100;
Math.round(dollar);
dollar = dollar / 100;
penny = (int) (dollar / 0.01);
System.out.println(buck + " x 1 Dollar");
System.out.println(half + " x 50 Cent");
System.out.println(quarter + " x 25 Cent");
System.out.println(dime + " x 10 Cent");
System.out.println(nickel + " x 5 Cent");
System.out.println(penny + " x 1 Cent");
}
}
Über das Programm hatte ich auch hier schon damals ein paar Fragen doch nun konnte ich es selbst so programmieren =)
LG