exercise2_14.java

来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 49 行

JAVA
49
字号
import javax.swing.JOptionPane;

public class Exercise2_14 {
  // Main method
  public static void main(String[] args) {
    int amount; // Amount entered from the keyboard

    // Receive the amount entered from the keyboard
    String amountString = JOptionPane.showInputDialog(
      "Enter an amount in integer, for example 1156 \nfor 11 dollars and 56 cents");

    // Convert string to double
    amount = Integer.parseInt(amountString);

    int remainingAmount = amount;

    // Find the number of one dollars
    int numOfOneDollars = remainingAmount / 100;
    remainingAmount = remainingAmount % 100;

    // Find the number of quaters in the remaining amount
    int numOfQuarters = remainingAmount / 25;
    remainingAmount = remainingAmount % 25;

    // Find the number of dimes in the remaining amount
    int numOfDimes = remainingAmount / 10;
    remainingAmount = remainingAmount % 10;

    // Find the number of nickels in the remaining amount
    int numOfNickels = remainingAmount / 5;
    remainingAmount = remainingAmount % 5;

    // Find the number of pennies in the remaining amount
    int numOfPennies = remainingAmount;

    // Display results
    System.out.println("Your amount " + amount + " consists of ");
    System.out.println(numOfOneDollars + "\t dollars");
    System.out.println(numOfQuarters + "\t quarters");
    System.out.println(numOfDimes + "\t dimes");
    System.out.println(numOfNickels + "\t nickels");
    System.out.println(numOfPennies + "\t pennies");

    System.exit(0);
  }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?