reward.java

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

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

/*  How many grains of rice does the Rajah place on 
 *  square n if the Rajah places one grain on 
 *  the first square of a chessboard, and 
 *  places double the amount of the previous 
 *  square on the next square, and so on. Allow
 *  the user to repeat for different n.
 */

import iopack.Io;
public class Reward {
  public static void main(String [] args) {
    long amount;
    char repeat;
    int squarenum;

    do {
      amount = 1; 
      squarenum = Io.readInt("Which square, 1-64?");
      if (squarenum == 1)
        System.out.println("The reward on square 1 is 1");
      else {    
        for (int i = 2; i <= squarenum; i++) 
          amount += amount;
        System.out.print("The reward on square " + squarenum);
        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 + -
显示快捷键?