⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cointoss.java

📁 Java 入门书的源码
💻 JAVA
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/*  The user guesses heads or tails
 *  We return a random number, interpreting
 *  a value from 0 to .5 as heads, and a value
 *  from .5 to 1.0 as tails
 */

import iopack.Io;
public class CoinToss {
  public static void main(String [] args)  {
    char guess;       //  the user's guess
    char coin;        //  the program's toss
    char repeat;      //  'Y' or 'y' to play again

    do {
      guess = Io.readChar("Enter 'H' for heads, 'T' for tails");
      if (Math.random() < .5)
        coin = 'H';
      else
        coin = 'T';
      if (guess == coin)
        System.out.println("You win!");
      else
        System.out.println("Computer wins");
      repeat = Io.readChar("Enter 'Y' to play again, '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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -