📄 readkb.java
字号:
// File: ReadKb.java
// Purpose: Read an int, a double and String from the keyboard
// Listing: Chapter 5, Listing 5.3
import java.io.*; // Access the I/O package
public class ReadKb {
public static double getDouble() { // Read a double
double d = 0.0;
String s = "";
try {
s = getString();
if (s == null)
s = "";
d = Double.parseDouble(s); // Convert s to d
}
catch (NumberFormatException nfe) { // Catch invalid data
System.out.println("Not a valid double: " + s);
}
return d;
}
public static int getInt() { // Read an int
int i = 0;
String s = "";
try {
s = getString();
i = Integer.parseInt(s); // Convert s to i
}
catch (NumberFormatException nfe) { // Catch invalid data
System.out.println("Not a valid integer: " + s);
}
return i;
}
public static String getString() { // Read a String
BufferedReader br;
InputStreamReader isr;
String s = "";
isr = new InputStreamReader(System.in); // Keyboard stream
br = new BufferedReader(isr); // Add line buffering
try {
s = br.readLine(); // Read a line
}
catch (IOException iox) { // Catch exceptions
System.out.println(iox);
}
return s;
}
public static void main(String[] args) {
double d;
int i;
String s;
System.out.print("Enter an integer: ");
i = ReadKb.getInt();
System.out.println("You entered: " + i);
System.out.print("Enter a double: ");
d = ReadKb.getDouble();
System.out.println("You entered: " + d);
System.out.print("Enter a string: ");
s = ReadKb.getString();
System.out.println("You entered: " + s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -