mercredi 30 septembre 2015

why does it print twice? if -else

I'm learning java atm , and had to write a code to calculate the monetary units, and only display the nonzero denominations using singular words for single units and plural words for plural units.

This is the code so far:

import java.util.Scanner;

public class ComputeChange {
    public static void main(String[] args) {

        Scanner input = new Scanner(System. in );
        // receive amount
        System.out.println("Enter an amount in double, for example 11.56: ");

        double amount = input.nextDouble();

        int remainingAmount = (int)(amount * 100);

        // find the number of one dollars
        int numberOfDollars = remainingAmount / 100;
        remainingAmount = remainingAmount % 100;

        // find the number of quarters in the remaing amount
        int numberOfQuarters = remainingAmount / 25;
        remainingAmount = remainingAmount % 25;

        //find the number of dimes in the remaing amount
        int numberOfDimes = remainingAmount / 10;
        remainingAmount = remainingAmount % 10;

        //find the number of nickels in the remaing amount 
        int numberOfNickles = remainingAmount / 5;
        remainingAmount = remainingAmount % 5;

        //find the number of pennies in the remaining amount
        int numberOfPennies = remainingAmount;

        //Display results
        System.out.println("Your amount" + amount + "consists of");

        if (numberOfDollars > 1) {
            System.out.println(" " + numberOfDollars + "dollars");
        } else if (numberOfDollars == 1); {
            System.out.println(" " + numberOfDollars + "dollar");
        }

The output is: run:

Enter an amount in double, for example 11.56: 
12,33
Your amount12.33consists of
 12dollars
 12dollar
 1quarters
 1quarter
 0dimes
 0dime
 1nickles
 1nickle
 3pennies
 3penny

Why is everything printed double? 3 == not 1 so why does it still say 3 penny? Noob question maybe, but thats because i am one :) Thanks for help!

Aucun commentaire:

Enregistrer un commentaire