当前位置: 代码迷 >> Java相关 >> [求助]标志控制循环,不知道哪出错了
  详细解决方案

[求助]标志控制循环,不知道哪出错了

热度:212   发布时间:2006-10-21 22:25:56.0
[求助]标志控制循环,不知道哪出错了
import java.io.*;
public class average1{
public static void main(String args[])throws IOException{
int counter,grade,total;
double average;
total=0;
counter=0;
System.out.print("Enter lettle grade,z to end: ");
System.out.flush();
grade=System.in.read();
while (grade!='z'){
if (grade=='A')
total=total+5;
else if (grade=='B')
total=total+4;
else if (grade=='C')
total=total+3;
else if (grade=='D')
total=total+2;
else if (grade=='F')
total=total+0;
System.in.skip(1);
counter=counter+1;
System.out.print("Enter lettle grade,z to end: ");
System.out.flush();
grade=System.in.read();
}
if (counter!=0){
average=(double)total/counter;
System.out.println("class average is:"+average);
}
else
System.out.println("No grade were entered");
}
}
上面的程序编译都没出问题,但运行后输入 A 后按回车键却出现下面的东西
Enter letter grade,z to end:Exception in thread"main" java.io.IOException:句柄无效
at java.io.FileInputStream.skip<Native Method>
at.java.io.BufferedInputStream.skip(BufferedInputStream.java:305>
at average1.main<average1.java:22)
请高手帮我解决这个问题啊
搜索更多相关的解决方案: grade  System  counter  public  java  

----------------解决方案--------------------------------------------------------
System.in.skip(1);
把这句去掉,就可以正常运行了

----------------解决方案--------------------------------------------------------
System.in.skip(1);冰峰问一下skip是那个类里面的??
----------------解决方案--------------------------------------------------------
输入流里面的
但是这里不适用
----------------解决方案--------------------------------------------------------

冰峰可以解释一下为什么加上System.in.skip(1);就出错吗??


----------------解决方案--------------------------------------------------------

skip
public long skip(long n)
throws IOExceptionSkips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.
The skip method of InputStream creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method.


Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

这是API上的说明
因为System.in这个输入流,是一个特珠的输入流,谁也不知道它将有多少东西将要被输入.


----------------解决方案--------------------------------------------------------

真是在冰峰这学到很多的东西啊!


----------------解决方案--------------------------------------------------------

冰峰,System.in是输入流,但它后面那个.skip(1)是什么意思?我还没看到过skip这东西


----------------解决方案--------------------------------------------------------

public long skip(long n)
throws IOException跳过和放弃此输入流中的 n 个数据字节。出于各种原因,该 skip 方法跳过某些较小的字节数(可能是 0)后结束。这可能由多种条件引起;在跳过 n 个字节之前已到达文件的末尾只是其中的一种可能。返回跳过的实际字节数。如果 n 为负,则不跳过任何字节。
InputStream 的 skip 方法创建字节数组,然后重复向其读入,直到读够 n 个字节或已到达流的末尾为止。建议让子类提供此方法的更有效的实现。


参数:
n - 要跳过的字节数。
返回:
跳过的实际字节数。


----------------解决方案--------------------------------------------------------
呵呵,都把API搬出来了
----------------解决方案--------------------------------------------------------
  相关解决方案