java - why does it print twice? if -else -
this question has answer here:
i'm learning java atm , , had write code calculate monetary units, , display nonzero denominations using singular words single units , plural words plural units.
this code 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 amount in double, example 11.56: "); double amount = input.nextdouble(); int remainingamount = (int)(amount * 100); // find number of 1 dollars int numberofdollars = remainingamount / 100; remainingamount = remainingamount % 100; // find number of quarters in remaing amount int numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; //find number of dimes in remaing amount int numberofdimes = remainingamount / 10; remainingamount = remainingamount % 10; //find number of nickels in remaing amount int numberofnickles = remainingamount / 5; remainingamount = remainingamount % 5; //find number of pennies in 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 amount in double, example 11.56: 12,33 amount12.33consists of 12dollars 12dollar 1quarters 1quarter 0dimes 0dime 1nickles 1nickle 3pennies 3penny
why printed double? 3 == not 1 why still 3 penny? noob question maybe, thats because 1 :) help!
because added random ;
after second if
. therefor second system.out.println
not part of if-statement. remove it:
if (numberofdollars > 1) { system.out.println (" " + numberofdollars + "dollars"); } else if (numberofdollars == 1) { system.out.println (" " + numberofdollars + "dollar"); }
Comments
Post a Comment