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

📄 testinputreader.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * TestInputReader.java  E.L. 2001-08-12
 */
import javax.swing.JOptionPane; 
class InputReader {  
  
  public static String inputText(String prompt) {
    String text = JOptionPane.showInputDialog(prompt);
    while (text == null || text.trim().equals("")) {
      JOptionPane.showMessageDialog(null, "You have to enter data!");
      text = JOptionPane.showInputDialog(prompt);
    }
    return text.trim();
  }
  
  public static int inputInteger(String prompt) {
    int number = 0;
    boolean ok = false;
    do {
      String theInputText = inputText(prompt);
      try {
        number = Integer.parseInt(theInputText);
        ok = true;
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid integer!\n");      
      }
    } while (!ok);
    return number;
  }

  public static double inputDecimalNumeral(String prompt) {
    double number = 0;
    boolean ok = false;
    do {
      String theInputText = inputText(prompt);
      try {
        number = Double.parseDouble(theInputText);
        ok = true;
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid decimal numeral!\n");
      }
    } while (!ok);
    return number;
  }
}

class TestInputReader {
  public static void main(String[] args) {
    String text = InputReader.inputText("Enter a text: ");
    double decimal = InputReader.inputDecimalNumeral("Enter a decimal numeral: ");
    int integer = InputReader.inputInteger("Enter an integer: ");
    System.out.println("You entered this: ");
    System.out.println(text);
    System.out.println(decimal);
    System.out.println(integer);
    System.exit(0);
  }
}

⌨️ 快捷键说明

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