readInt 的原码如下,它一次性读入了四个 int 。 /** * See the general contract of the <code>readInt</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @return the next four bytes of this input stream, interpreted as an * <code>int</code>. * @exception EOFException if this input stream reaches the end before * reading four bytes. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public final int readInt() throws IOException { InputStream in = this.in; int ch1 = in.read(); int ch2 = in.read(); int ch3 = in.read(); int ch4 = in.read(); if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException(); return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); }
还是不行,结果稀奇古怪 能不能给了例子怎样从System.in中读去int,long等类型的值
前面的老兄说得对, 通过字符串方式读进程序,然后在把字符串转换成你需要的类型,例如转换成整数Integer.parseInt( s );假如你输入的字符串不是整数类型,将抛出异常
* See the general contract of the <code>readInt</code>
* method of <code>DataInput</code>.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next four bytes of this input stream, interpreted as an
* <code>int</code>.
* @exception EOFException if this input stream reaches the end before
* reading four bytes.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterInputStream#in
*/
public final int readInt() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
能不能给了例子怎样从System.in中读去int,long等类型的值
通过字符串方式读进程序,然后在把字符串转换成你需要的类型,例如转换成整数Integer.parseInt( s );假如你输入的字符串不是整数类型,将抛出异常
无论你想读取输入的数据是哪个类型的,必须先用readLine()读成字符串先
再转换成你需要的类型,如果数据和格式不合,则需要捕捉异常
没有其它的方法