formattedinput.java
来自「非常好的java事例以及带源码事例的java2教程」· Java 代码 · 共 42 行
JAVA
42 行
import java.io.*;
public class FormattedInput
{
// Method to read an int value
public int intRead()
{
try
{
for(int i = 0; i < 5; i++)
{
if(tokenizer.nextToken() == tokenizer.TT_NUMBER &&
tokenizer.nval==(double)((long)tokenizer.nval))
return (int)tokenizer.nval; // Value is integral, so return as int
else
{
System.out.println("Incorrect input: " + tokenizer.sval +
" Re-enter an integer");
continue; // Retry the read operation
}
}
System.out.println("Five failures reading an int value" +
" - program terminated");
System.exit(1); // End the program
return 0;
}
catch(IOException e) // Error reading in nextToken()
{
System.out.println(e); // Output the error
System.exit(1); // End the program
return 0;
}
}
// plus methods to read various other data types...
// Object to tokenize input from the standard input stream
private StreamTokenizer tokenizer = new StreamTokenizer(
new InputStreamReader(System.in));
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?