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

📄 usermenu.java

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

/*  Use a switch statement to input a user's selection of
 *  an alternative. Uses code from Examples 3.5 and 3.6.
 */
 
import iopack.Io;
public class UserMenu {
  public static void sum() {
    int score;              // holds current score
    int answer;             // 1 if another score
                            // 0 if no more scores
    int total = 0;          // sum of scores so far
    answer = Io.readInt("Enter 1 to enter score, 0 to quit");
    while (answer == 1) {
      score = Io.readInt("Enter the score");
      total += score;
      answer = Io.readInt("Enter 1 to enter score, 0 to quit");
    }
    System.out.println("The total is " + total);
  }

  public static void max() {
    double number;            // the next number 
    double maxSoFar;       //  the max so far
    System.out.println("Enter nonnegative floating-point numbers");
    System.out.println
           ("Enter a negative number to terminate the input");
    number = Io.readDouble("Enter the first number");
    if (number >= 0) {
      maxSoFar = number;
      while (number >= 0) {
        number = Io.readDouble("Enter the next number");
        if (number > maxSoFar) 
          maxSoFar = number;
      }
      System.out.println("The maximum is " + maxSoFar);
    }
    else
      System.out.println("No input provided");
  }

  public static void printMenu() {
    System.out.println();
    System.out.println("Choose from the following list");
    System.out.println("1.  Find the sum of test scores");
    System.out.println("2.  Find the maximum test score");
    System.out.println("3.  Do something else");
    System.out.println("4.  Quit");
  }

  public static void main(String [] args) {
    printMenu();
    int choice = Io.readInt("Enter your choice, 1, 2, 3 or 4");
    while (choice != 4) {
      switch (choice) {
        case 1:  
          sum();
          break;
        case 2:
          max();
          break;
        case 3:
          System.out.println("Fill in code here");
          break;
      }
      printMenu();
      choice = Io.readInt("Enter your choice, 1, 2, 3 or 4");
    }
    Io.readString("Press any key to exit");   // Added for IDE use
  }
}

⌨️ 快捷键说明

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