Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. //import java.util.Scanner;
  2. import javax.swing.JOptionPane;
  3.  
  4. public class Lab3YunpingCui {
  5.   public static void main(String[] args) {
  6.     // Create a Scanner
  7. //    Scanner input = new Scanner(System.in);
  8.  
  9.     // Receive the amount
  10. //    System.out.print("Enter an amount in double, for example 11.56: ");
  11. //    double amount = input.nextDouble();
  12.  
  13.     String astr = JOptionPane.showInputDialog(null, "Enter an amount in double, for example 11.56");
  14.     double amount = Double.parseDouble(astr);
  15.  
  16.     int remainingAmount = (int)(amount * 100);
  17.  
  18.     // Find the number of one dollars
  19.     int numberOfOneDollars = remainingAmount / 100;
  20.     remainingAmount = remainingAmount % 100;
  21.  
  22.     // Find the number of quarters in the remaining amount
  23.     int numberOfQuarters = remainingAmount / 25;
  24.     remainingAmount = remainingAmount % 25;
  25.  
  26.     // Find the number of dimes in the remaining amount
  27.     int numberOfDimes = remainingAmount / 10;
  28.     remainingAmount = remainingAmount % 10;
  29.  
  30.     // Find the number of nickels in the remaining amount
  31.     int numberOfNickels = remainingAmount / 5;
  32.     remainingAmount = remainingAmount % 5;
  33.  
  34.     // Find the number of pennies in the remaining amount
  35.     int numberOfPennies = remainingAmount;
  36.  
  37.     // Display results
  38.     //System.out.println("Your amount " + amount + " consists of");
  39.      String msg = "Your amount " + amount + " consists of";
  40.      
  41.      if (amount == 0.0) {
  42.          msg = msg + "\n" + ("\tNothing");
  43.      }
  44.  
  45.      if (numberOfOneDollars == 1) {
  46.          //System.out.println("\t" + numberOfOneDollars + " dollar");
  47.          msg = msg + "\n" + ("\t" + numberOfOneDollars + " dollar");
  48.      } else if (numberOfOneDollars > 1) {
  49.          //System.out.println("\t" + numberOfOneDollars + " dollars");
  50.          msg = msg + "\n" + ("\t" + numberOfOneDollars + " dollars");
  51.      }
  52.  
  53.      if (numberOfQuarters == 1) {
  54.          //System.out.println("\t" + numberOfQuarters + " quarter");
  55.          msg = msg + "\n" + ("\t" + numberOfQuarters + " quarter");
  56.      } else if (numberOfQuarters > 1) {
  57.          //System.out.println("\t" + numberOfQuarters + " quarters");
  58.          msg = msg + "\n" + ("\t" + numberOfQuarters + " quarters");
  59.      }
  60.  
  61.      if (numberOfDimes == 1) {
  62.          //System.out.println("\t" + numberOfDimes + " dime");
  63.          msg = msg + "\n" + ("\t" + numberOfDimes + " dime");
  64.      } else if (numberOfDimes > 1) {
  65.          //System.out.println("\t" + numberOfDimes + " dimes");
  66.          msg = msg + "\n" + ("\t" + numberOfDimes + " dime");
  67.      }
  68.  
  69.      if (numberOfNickels == 1) {
  70.          //System.out.println("\t" + numberOfNickels + " nickel");
  71.          msg = msg + "\n" + ("\t" + numberOfNickels + " nickel");
  72.      } else if (numberOfNickels > 1) {
  73.          //System.out.println("\t" + numberOfNickels + " nickels");
  74.          msg = msg + "\n" + ("\t" + numberOfNickels + " nickels");
  75.      }
  76.  
  77.      if (numberOfPennies == 1) {
  78.          //System.out.println("\t" + numberOfPennies + " pennie");
  79.          msg = msg + "\n" + ("\t" + numberOfPennies + " pennie");
  80.      } else if (numberOfPennies > 1) {
  81.          //System.out.println("\t" + numberOfPennies + " pennies");
  82.          msg = msg + "\n" + ("\t" + numberOfPennies + " pennies");
  83.      }
  84.      
  85.      JOptionPane.showMessageDialog(null, msg);
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement