choice.java

来自「Beginning Java 2, SDK 1.4 Edition Exerci」· Java 代码 · 共 41 行

JAVA
41
字号
//Chapter 3, Exercise 1

public class Choice {
  public static void main(String args[]) {
    // Use an integer to determine the choice:
    int myChoice = 0;

    // Select a random value for myChoice between 0 and 5:
    myChoice = (int)(6.0*Math.random());
    
    // The above line ensures each integer from 0 to 5 is equiprobable. 
    // Multiplying the value produced by random() by 6.0 results in a value
    // from 0.0 to 5.99999
    // Values from 0 to 0.99999 becomes 0, from 1.00 to 1.99999 becomes 1,
    // and so on, with values from 5.0 to 5.99999 becoming 5.
    
    // Print the appropriate Breakfast Choice depending on value myChoice:
    switch(myChoice) {
      case 0:
        System.out.println("Breakfast choice is scrambled eggs");
        break;
      case 1:
        System.out.println("Breakfast choice is waffles");
        break;
      case 2:
        System.out.println("Breakfast choice is fruit");
        break;
      case 3:
        System.out.println("Breakfast choice is cereal");
        break;
      case 4:
        System.out.println("Breakfast choice is toast");
        break;
      case 5:
        System.out.println("Breakfast choice is yogurt");
        break;
    }
  }
}

⌨️ 快捷键说明

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