printf und (int)

ocsme

Top Contributor
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 😀

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
 
Verwende besser nur Cent und keine Floats oder Doubles. Wenn du in dollar ausgeben willst teile bei der Ausgabe durch 100.
Beispiel:
Java:
int cent=1050;
String dollar=cent/100+","+(cent-(cent/100)*100)+"$";
 
Zuletzt bearbeitet von einem Moderator:
Habe das mal umgeschrieben:
Java:
        // double dollar = Math.random() * 10;
        double dollar = 7.90543;
        System.out.println("dollar = " + dollar);
        int buck;
        int half;
        int quarter;
        int dime;
        int nickel;
        int penny;

        double cent = (dollar - (int) dollar) * 100;
        buck = (int) dollar;

        half = (int) Math.round(cent) / 50;
        cent = (int) Math.round(cent) % 50;

        quarter = (int) Math.round(cent) / 25;
        cent = (int) Math.round(cent) % 25;

        dime = (int) Math.round(cent) / 10;
        cent = (int) Math.round(cent) % 10;

        nickel = (int) Math.round(cent) / 5;
        cent = (int) Math.round(cent) % 5;

        penny = (int) Math.round(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");

Code:
dollar = 7.90543
7 x 1 Dollar
1 x 50 Cent
1 x 25 Cent
1 x 10 Cent
1 x 5 Cent
1 x 1 Cent

Bei der Gelgenheit ruhig mal gönnen: http://stackoverflow.com/questions/2947044/how-do-i-use-modulus-for-float-double

Ich sag nicht, dass das das Beste ist, aber nahe daran. 😉
 
@DerWissende Ich hab mir mal erlaubt dein Programm etwas zu "pimpen", denn man kann sich einige Rundungsoperationen sparen:
Java:
double dollar = 7.90543;
System.out.println("dollar = " + dollar);

int buck = (int) dollar;

double cent = (dollar - (int) dollar) * 100d;

int half = (int) Math.round(cent) / 50;
cent -= half * 50;

int quarter = (int) Math.round(cent) / 25;
cent -= quarter * 25;

int dime = (int) Math.round(cent) / 10;
cent -= dime * 10;

int nickel = (int) Math.round(cent) / 5;
cent -= nickel * 5;

int penny = (int) Math.round(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");

Was ist passiert? Nachdem du herausgefunden hast wie viel ganze Münzen noch enthalten sind, kannst du diese dann ja abziehen und die nächsten Wertigkeiten berechenen.
 
Tun wir etwas total verrücktes:
Java:
        // double dollar = Math.random() * 10;
        double dollar = 7.90543;
        System.out.println("dollar = " + dollar);
        int cent = (int) Math.round(dollar * 100.0);
        System.out.println("cent   = " + cent);
        int[] array1 = {
            100,
            50,
            25,
            10,
            5,
            1
        };
        int[] array2 = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            array2[i] = cent / array1[i];
            cent %= array1[i];
        }
        System.out.println(array2[0] + " x 1 Dollar");
        System.out.println(array2[1] + " x 50 Cent");
        System.out.println(array2[2] + " x 25 Cent");
        System.out.println(array2[3] + " x 10 Cent");
        System.out.println(array2[4] + " x 5 Cent");
        System.out.println(array2[5] + " x 1 Cent");

Kommt vom ergebnis her auch auf das gleiche raus, aber ähnelt jetzt nicht mehr den ursprünglichen.

Bearbeitung: Oder auch nur mit 1 Array (nicht sehr sinnvoll):
Java:
        double dollar = Math.random() * 10;
        // double dollar = 7.90543;
        System.out.println("dollar = " + dollar);
        int cent = (int) Math.round(dollar * 100.0);
        System.out.println("cent   = " + cent);
        int[] array1 = {
            100,
            50,
            25,
            10,
            5,
            1
        };
        for (int i = 0; i < array1.length; i++) {
            int temp = cent / array1[i];
            cent %= array1[i];
            array1[i] = temp;
        }
        System.out.println(array1[0] + " x 1 Dollar");
        System.out.println(array1[1] + " x 50 Cent");
        System.out.println(array1[2] + " x 25 Cent");
        System.out.println(array1[3] + " x 10 Cent");
        System.out.println(array1[4] + " x 5 Cent");
        System.out.println(array1[5] + " x 1 Cent");
 
Zuletzt bearbeitet von einem Moderator:

Zurück
Oben