本帖最后由 Fantasy_. 于 2012-11-25 22:32 编辑
import java.util.Scanner;
public class getLevel {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
getLevelOfNumber gl = new getLevelOfNumber();//创建读取分数的实例
System.out.println("请输入您的分数:");
int num = input.nextInt(); //读取分数
System.out.println("您的分数是:"+ num +"\n您的分数等级是:"+ gl.levelOfNumber(num)); //打印分数跟等级
}
}
//把分数对应的等级封装起来
class getLevelOfNumber{
private String level; //用 private提高安全性 ,使得除 getLevelOfNumber类之外的类不能够直接调用level
public String levelOfNumber(int n){ //String 是双引号("A")的。char是单引号('A')的。
if (n >= 90 && n <= 100)
level = "A";
else if (n >= 80 && n < 90)
level = "B";
else if (n >= 70 && n < 80)
level = "C";
else if (n >= 60 && n < 70)
level = "D";
else
level = "E";
return level;
}
}
/*
char的写法:
class getLevelOfNumber{
private char level;
public char levelOfNumber(int n){
if (n >= 90 && n <= 100)
level = 'A';
else if (n >= 80 && n < 90)
level = 'B';
else if (n >= 70 && n < 80)
level = 'C';
else if (n >= 60 && n < 70)
level = 'D';
else
level = 'E';
return level;
}
}
*/
|