public static int readInt() throws IOException,NumberFormatException{ return Integer.parseInt(readString().trim()); }
public static byte readByte() throws IOException,NumberFormatException{ return Byte.parseByte(readString().trim()); }
public static short readShort() throws IOException,NumberFormatException{ return Short.parseShort(readString().trim()); }
public static long readLong() throws IOException,NumberFormatException{ return Long.parseLong(readString().trim()); }
public static float readFloat() throws IOException,NumberFormatException{ return Float.parseFloat(readString().trim()); }
public static double readDouble() throws IOException,NumberFormatException{ return Double.parseDouble(readString().trim()); }
public static boolean readBoolean() throws IOException,NumberFormatException{ return (new Boolean(readString().trim())).booleanValue(); }}
class MyClass extends 楼上.StdInput{}
BufferReader reader = new BufferReader(new InputStreamReader(System.in)); int a = reader.read(); // read int String line = reader.readLine(); // read a lineDataInputStream reader = new DataInputStream(System.in); int i = reader.read(); // read int char c = reader.readChar(); // read char double d = reader.readDouble(); // read double ... // you could see the jdk String str = reader.readUTF(); // read stringand you could use ObjectInputStream also
不用extends 直接 StdInput.readXXX() 就可以了
楼上的有没有试过DataInputStream的使用?import java.io.*; public class OutPrint{ public static void main(String args[])throws IOException { System.out.println("Enter number:"); DataInputStream br=new DataInputStream(System.in); int a=br.readInt(); System.out.println("a="+a); } } Java的文档,DataInputStream类的方法readInt()的解释如下:Reads four input bytes and returns an int value. Let a be the first byte read, b be the second byte, c be the third byte, and d be the fourth byte. The value returned is:
This method is suitable for reading bytes written by the writeInt method of interface DataOutput. 意为从输入流中取联系的四个字节,第一个为最高字节,第二个为次高字节,依次类推,组成一个整数。它适合于读取使用DataOutputStream类中的writeInt()方法保存的数据。在上面的程序中,若输入142<回车>,则得到的字节流为3134320D0A,即为3个数字的ASCII码和回车和换行的ASCII码;若输入45<回车>,则字节流为34350D0A,即两个数字字符的ASCII码加回车和换行的ASCII码;若输入0<回车>2<回车>,则字节流为300D0A320D0A。均取前四个字节组成一个整数。
String str = br.readLine();如果需要的是int、double等,再对str进行parse
Int max=Integer.parseInt(br.readline());楼上说的正确~!
public static String readString() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
public static int readInt() throws IOException,NumberFormatException{
return Integer.parseInt(readString().trim());
}
public static byte readByte() throws IOException,NumberFormatException{
return Byte.parseByte(readString().trim());
}
public static short readShort() throws IOException,NumberFormatException{
return Short.parseShort(readString().trim());
}
public static long readLong() throws IOException,NumberFormatException{
return Long.parseLong(readString().trim());
}
public static float readFloat() throws IOException,NumberFormatException{
return Float.parseFloat(readString().trim());
}
public static double readDouble() throws IOException,NumberFormatException{
return Double.parseDouble(readString().trim());
}
public static boolean readBoolean() throws IOException,NumberFormatException{
return (new Boolean(readString().trim())).booleanValue();
}}
int a = reader.read(); // read int
String line = reader.readLine(); // read a lineDataInputStream reader = new DataInputStream(System.in);
int i = reader.read(); // read int
char c = reader.readChar(); // read char
double d = reader.readDouble(); // read double
... // you could see the jdk
String str = reader.readUTF(); // read stringand you could use ObjectInputStream also
public class OutPrint{
public static void main(String args[])throws IOException {
System.out.println("Enter number:");
DataInputStream br=new DataInputStream(System.in);
int a=br.readInt();
System.out.println("a="+a);
}
}
Java的文档,DataInputStream类的方法readInt()的解释如下:Reads four input bytes and returns an int value. Let a be the first byte read, b be the second byte, c be the third byte, and d be the fourth byte. The value returned is:
(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))
This method is suitable for reading bytes written by the writeInt method of interface DataOutput. 意为从输入流中取联系的四个字节,第一个为最高字节,第二个为次高字节,依次类推,组成一个整数。它适合于读取使用DataOutputStream类中的writeInt()方法保存的数据。在上面的程序中,若输入142<回车>,则得到的字节流为3134320D0A,即为3个数字的ASCII码和回车和换行的ASCII码;若输入45<回车>,则字节流为34350D0A,即两个数字字符的ASCII码加回车和换行的ASCII码;若输入0<回车>2<回车>,则字节流为300D0A320D0A。均取前四个字节组成一个整数。