import java.io.*;
public class Average { public static void main(String[] args) throws IOException {
int counter, grade, total;
double average;
//initialization phase
total = 0;
counter = 1;
// processing phase
while( counter <= 10){
System.out.print( "Enter lette grade: " );
System.out.flush();
grade = System.in.read();
System.out.println("grade1 is " +grade );//test the result
if( grade == 'A')
total = total + 4;
else if( grade == 'B')
total = total + 3;
else if( grade == 'C')
total = total + 2;
else if( grade == 'D')
total = total + 1;
else if( grade == 'E')
total = total + 0;
System.in.skip( 1 ); //为什么非要跳过两个字符!
counter = counter + 1;
System.out.println("grade2 is " +grade );//test the result
}
//termination phase
average = (double)total / 10; //integer division
System.out.println( "Class average is " + average ); }
} 本人初学JAVA,对Skip()方法甚是不解,为什么非要跳过2个字符,不跳又要报错,望高人指点一二!
public class Average { public static void main(String[] args) throws IOException {
int counter, grade, total;
double average;
//initialization phase
total = 0;
counter = 1;
// processing phase
while( counter <= 10){
System.out.print( "Enter lette grade: " );
System.out.flush();
grade = System.in.read();
System.out.println("grade1 is " +grade );//test the result
if( grade == 'A')
total = total + 4;
else if( grade == 'B')
total = total + 3;
else if( grade == 'C')
total = total + 2;
else if( grade == 'D')
total = total + 1;
else if( grade == 'E')
total = total + 0;
System.in.skip( 1 ); //为什么非要跳过两个字符!
counter = counter + 1;
System.out.println("grade2 is " +grade );//test the result
}
//termination phase
average = (double)total / 10; //integer division
System.out.println( "Class average is " + average ); }
} 本人初学JAVA,对Skip()方法甚是不解,为什么非要跳过2个字符,不跳又要报错,望高人指点一二!
object必选项。总是某个 TextStream 对象的名称。characters必选项。在读取文件时要跳过的字符个数。说明
被跳过的字符即被放弃。下面的示例演示了Skip 方法的用法:function SkipDemo()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject")
f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
f.WriteLine("Hello world!");
f.WriteLine("JScript is fun");
f.Close();
f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
f.Skip(6);
r = f.ReadLine();
return(r);
}资料引用:http://www.knowsky.com/368850.html
如果是:你输入A然后回车
实际打入的字符串是A\r\n
第一次read进去了A
如果不跳过2个字节
你把skip那句话注释掉
你会发现读入了13和10
就是读入了\r和\n所以需要跳过这两个你不需要的字节
即当你输入ABC的话,只返回A的ASCII值,你回车之后,流中就包含
'\r'(即回车,windows下),'\n'(换行),所以你得跳过这两个字节,否则下次循环
就会返回的是'\r'啦,综合5楼的所说的,应该差不多了