最好是能帮我改下并解释下为什么,谢谢啦,我对流感觉好陌生,感觉好难看的懂。(^_^)!
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class stream {
public static void main(String[] args) {
FormattedInput kb=new FormattedInput();
for(int i=0;i<5;i++)
{
try {
System.out.print("enter an int:");
System.out.println("int read"+kb.readInt());
System.out.print("enter double value:");
System.out.print(" double:"+kb.readInt());
System.out.print("enter an string:");
System.out.print("string:"+kb.readInt());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
class FormattedInput
{
private int readToken()
{
try {
ttype=tokenizer.nextToken();
return ttype;
}
catch (IOException ex) {
ex.printStackTrace(System.err);
System.exit(0);
}
return 0;
}
private StreamTokenizer tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private int ttype;
public int readInt()throws Exception
{
if(readInt()!=tokenizer.TT_NUMBER)
{
throw new Exception("readInt()failed."+"Input data not number");
}
return (int) tokenizer.nval;
}
}
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class stream {
public static void main(String[] args) {
FormattedInput kb=new FormattedInput();
for(int i=0;i<5;i++)
{
try {
System.out.print("enter an int:");
System.out.println("int read"+kb.readInt());
System.out.print("enter double value:");
System.out.print(" double:"+kb.readInt());
System.out.print("enter an string:");
System.out.print("string:"+kb.readInt());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
class FormattedInput
{
private int readToken()
{
try {
ttype=tokenizer.nextToken();
return ttype;
}
catch (IOException ex) {
ex.printStackTrace(System.err);
System.exit(0);
}
return 0;
}
private StreamTokenizer tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private int ttype;
public int readInt()throws Exception
{
if(readInt()!=tokenizer.TT_NUMBER)
{
throw new Exception("readInt()failed."+"Input data not number");
}
return (int) tokenizer.nval;
}
}
public int readInt() throws Exception
{
if (readInt() != tokenizer.TT_NUMBER)
{
throw new Exception("readInt()failed." + "Input data not number");
}
return (int) tokenizer.nval;
}readInt方法在这里自己调自己,形成一个死递归,直到溢出喵~~`
System.out.println("int read"+kb.readInt());
System.out.print("enter double value:");
System.out.print(" double:"+kb.readInt());
System.out.print("enter an string:");
System.out.print("string:"+kb.readInt());
我输入一个它就帮我输出一个(^_^)!