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

📄 io.java

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

package iopack;

import java.io.*;
import java.text.*;

public class Io {
  public static int readInt(String prompt) {
    boolean done = false;
    String s;
    int i=0;
    while (!done) {
      System.out.print(prompt+ ":  ");
      System.out.flush(); 
      try {
        BufferedReader in = new BufferedReader (
                              new InputStreamReader(System.in));
        s = in.readLine();
        i = Integer.parseInt(s);
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
        System.out.println("Error -- input an integer -- Try again");
      }  
    } 
    return i;
  }
  public static long readLong(String prompt) {
    boolean done = false;
    String s;
    long i=0;
    while (!done) {
      System.out.print(prompt+ ":  ");
      System.out.flush(); 
      try {
        BufferedReader in = new BufferedReader (
                              new InputStreamReader(System.in));
        s = in.readLine();
        i =  Long.parseLong(s);
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
        System.out.println("Error -- input a long, without an L suffix -- Try again");
      }  
    } 
    return i;
  } 
  public static double readDouble(String prompt) {
    boolean done = false;
    String s;
    double d=0;
    while (!done) {
      System.out.print(prompt+ ":  ");
      System.out.flush(); 
      try {
        BufferedReader in = new BufferedReader (
                              new InputStreamReader(System.in));
        s = in.readLine();
        d = new Double(s).doubleValue();
        done = true; 
      }catch (IOException e){
        done = true;
      }catch (NumberFormatException e1){ 
        System.out.println("Error -- input a double -- Try again");
     }  
    } 
    return d;
  } 
  public static void print$(double d) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    System.out.print(nf.format(d) + "  ");
  }
  public static void println$(double d) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    System.out.println(nf.format(d) + "  ");
  }
  public static void print(double d, int n) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(n);
    System.out.print(nf.format(d) + "  ");
  }   
  public static void println(double d, int n) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(n);
    System.out.println(nf.format(d) + "  ");
  }        
  public static char readChar(String prompt) {
    boolean done = false;
    String s;
    char c = ' ';
    while (!done) {
      System.out.print(prompt+ ":  ");
      System.out.flush(); 
      try {
        BufferedReader in = new BufferedReader (
                              new InputStreamReader(System.in));
        s = in.readLine();
        c =  s.charAt(0);
        done = true; 
      }catch (IOException e){
        done = true;
      }
    } 
    return c;
  } 
  public static String readString(String prompt) {
    boolean done = false;
    String s = "";
    while (!done) {
      System.out.print(prompt+ ":  ");
      System.out.flush(); 
      try {
        BufferedReader in = new BufferedReader (
                              new InputStreamReader(System.in));
        s = in.readLine();
        done = true; 
      }catch (IOException e){
        done = true;
      }
    } 
    return s;
  } 
  public static void main(String [] args) {
    System.out.println(readString("Enter a string "));
    System.out.println(readInt("Enter an integer "));
    System.out.println(readLong("Enter a long without an L suffix "));
    System.out.println(readChar("Enter a single character"));
    double d = readDouble("Enter a double ");
    print$(d);
    System.out.println();
    System.out.println(d);
    print(d,3);
    print(d, 1);
    print(d, 0);
    print(d, 5);
    System.out.println();
  }

} 

⌨️ 快捷键说明

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