lundi 19 décembre 2016

else if there is a dozen

I just wanted to know if there was a way i could put in a 'else if' statement or change my code in any way so if the user only put in a number that can be divided by 12(with nothing left over) I can make it output something else? It is not needed but I just think it will look nicer. Still learning so please don't make it too complicated

    //setting variables
    final int DOZEN = 12;
    final double PRICE_PER_DOZEN = 3.25;
    final double PRICE_PER_EGG = 0.45;
    int eggs;
    int dozens;
    int leftOvers;
    double total;

    //collecting input from user
    //setting the input to a variable
    Scanner input = new Scanner(System.in);
    System.out.print("Enter eggs needed >>");
    eggs = input.nextInt();

    //Calculation
    if (eggs < DOZEN)
    {
        total = eggs * PRICE_PER_EGG;
        System.out.println("You ordered " + eggs + " eggs's. That will   be a total off "
                + total + "$. Thankyou! Have a great day.");
    }
    else
    {
        dozens = eggs / DOZEN;
        leftOvers = eggs % DOZEN;
        total = dozens * PRICE_PER_DOZEN + leftOvers * PRICE_PER_EGG;

        System.out.println("You ordered " + eggs + " eggs. That's "
                + dozens + " dozen at $" + PRICE_PER_DOZEN +
                " per dozen and " + leftOvers + " loose eggs at " +
                (int) (PRICE_PER_EGG * 100) + " cents each for a total of $"
                + total);
    }

Aucun commentaire:

Enregistrer un commentaire