archonLing
发贴: 57
积分: 10
|
于 2003-09-23 09:53
I just want to make several points regarding your program:
1. there is a typo in the program, use "toal+=4;" instead "total=+4;". 2. java is very strong in string process. It would easier to use BufferedReder's readLine() function instead of trying manipulate the byte[] inputs when you are dealing with pure String inputs. 3. average calcuation is usually done in float or double in stead of int.
Have fun... -----------------------------------------------------------------------------------------------
import java.io.*;
public class Average { public static void main ( String args[] ) throws IOException { int counter,total,average; String grade;
//counter为计数器,grade储存输入的字符,total,average表示总数和平均数
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
total=0; counter=1;
while(counter<=10) { System.out.print("输入成绩级别(A,B,C,D,E):"); System.out.flush(); grade= stdin.readLine().trim();
if("A".equals(grade)) total+=4; else if("B".equals(grade)) total+=3; else if("C".equals(grade)) total+=2; else if("D".equals(grade)) total+=1; else if("E".equals(grade)) total+=0;
//System.in.skip(1); counter+=1; }
average=total/10; System.out.println("平均积分为"+average); } }
|