bigreward.java

来自「Java 入门书的源码」· Java 代码 · 共 34 行

JAVA
34
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/*  Solves the reward problem of Example 4.8
 *  using the BigInteger class to represent
 *  large numbers.
 */

import java.math.BigInteger;
import iopack.Io;

public class BigReward {
  public static void main(String [] args) {
    BigInteger amount;
    char repeat;
    int daynum;
                   
    do {
      amount = new BigInteger("1");
      daynum = Io.readInt("Reward for which day?");
      if (daynum == 1)
        System.out.println("The reward on day 1 is 1");
      else {    
        for (int i = 2; i <= daynum; i++) 
          amount = amount.add(amount);
        System.out.print("The reward on day " + daynum);
        System.out.println(" is " + amount);
      } 
      repeat = Io.readChar("Enter 'Y' to repeat, 'N' to quit");  
    } while (repeat == 'Y' || repeat == 'y');
    Io.readString("Press any key to exit");   // Added for IDE use
  }
}

⌨️ 快捷键说明

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