exercise5_17.java

来自「Introduction to java programming 一书中所有编程」· Java 代码 · 共 41 行

JAVA
41
字号
// Exercise5_17.java: Finding sales amount to meet the commission
public class Exercise5_17 {
  // The commission sought
  final static double COMMISSION_SOUGHT = 25000;

  // Main method
  public static void main(String[] args) {
    double commission = 0;
    double salesAmount = 1;
    int low = 0;
    int high = (int)(COMMISSION_SOUGHT / 0.08);

    while (low < high - 1) {
      int mid = (low + high) / 2;

      // Compute commission
      if (mid >= 10001)
        commission = 5000 * 0.08 + 5000 * 0.1 + (mid - 10000) * 0.12;
      else if (mid >= 5001)
        commission = 5000 * 0.08 + (mid - 5000) * 0.10;
      else
        commission = mid * 0.08;

      if (commission == COMMISSION_SOUGHT) {
        break;
      }
      else if (commission < COMMISSION_SOUGHT) {
        low = mid;
      }
      else {
        high = mid;
      }
    }

    // Display the sales amount
    System.out.println("The sales amount " + high +
      " is needed to make a commission of $" + COMMISSION_SOUGHT);
  }
}

⌨️ 快捷键说明

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