📄 chapter19n1.java
字号:
/** * program to illustrate simple i/o to the user's terminal */import java.io.* ;import java.lancs.* ;import java.util.* ;public class Chapter19n1 { public static void main(String[] args) throws IOException { // find out what sort of object the standard i/o streams are System.out.println("System.in is a " + System.in.getClass().getName()) ; System.out.println("System.out is a " + System.out.getClass().getName()) ; System.out.println("System.err is a " + System.err.getClass().getName()) ; System.out.println() ; // simple use of print/ln System.out.print("to be or not to be") ; System.out.println("that is the question") ; System.out.println() ; System.out.println("to be or not to be") ; System.out.println("that is the question") ; System.out.println() ; System.out.print("to be or not to be") ; System.out.println() ; System.out.println("that is the question") ; System.out.println() ; // indirect use of the "toString" method Date now = new Date() ; System.out.println("today's date is " + now) ; System.out.println() ; // output of numerical values System.out.println("**" + 123 + "**") ; System.out.println("**" + 456 + "**") ; System.out.println("**" + (double) 123456789 + "**") ; System.out.println("**" + (double) 1.23456 + "**") ; System.out.println("**" + (double) 12.3456 + "**") ; System.out.println("**" + (double) 123.456 + "**") ; System.out.println("**" + (double) 1234.56 + "**") ; System.out.println("**" + (double) 12345.6 + "**") ; System.out.println("**" + (double) 123.456789 + "**") ; System.out.println("**" + (double) 1234567.89 + "**") ; System.out.println() ; // use of the "flush" method and the "err" stream System.err.print("type in an integer value ") ; System.err.flush() ; int x = BasicIo.readInteger() ; System.out.println("the value of " + x + " squared is " + x * x) ; System.out.println() ; BufferedReader din = new BufferedReader(new InputStreamReader(System.in)) ; System.err.println("type in some lines, please") ; while (true) { String line = din.readLine() ; if (line == null) { System.err.println("fell off the end of the file") ; break ; } line = line.trim() ; if (line.length() == 0) break ; System.out.println("the line was **" + line + "**") ; } System.out.println("**end of input**") ; System.out.println() ; System.err.println("type in some more lines (in tokens), please") ; while (true) { String line = din.readLine().trim() ; if (line.length() == 0) break ; StringTokenizer st = new StringTokenizer(line) ; // StringTokenizer st = new StringTokenizer(line, "|") ; // StringTokenizer st = new StringTokenizer(line, " ,.") ; while (st.hasMoreTokens()) System.out.println("the token was **" + st.nextToken() + "**") ; } System.out.println("**end of input**") ; System.out.println() ; System.err.println("type an integer followed by a double (2 * i)") ; boolean invalidInput = false ; int firstValue = 0 ; double secondValue = 0.0 ; do { String line = din.readLine().trim() ; StringTokenizer st = new StringTokenizer(line) ; try { firstValue = Integer.parseInt(st.nextToken()) ; secondValue = Double.valueOf(st.nextToken()).doubleValue() ; invalidInput = !isInputOK(firstValue, secondValue) ; } catch (NumberFormatException e) { invalidInput = true ; } if (invalidInput) System.err.println("ERROR: retype both values") ; } while (invalidInput) ; System.out.println("the first value is " + firstValue) ; System.out.println("the second value is " + secondValue) ; System.out.println() ; } // end of main method public static boolean isInputOK(int a, double b) { return (b == 2 * a) ; } // end of method isInputOK } // end of class Chapter19n1.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -