java编写程序 4. 编写程序,从键盘输入学生成绩,根据成绩判断:如果成绩小于60,则输出“不及格”;若成

编写程序,从键盘输入学生成绩,根据成绩判断:如果成绩小于60,则输出“不及格”;若成绩在60和75之间,应输出“及格”;若成绩在75和90之间,应输出“良”;若成绩大于90,应输出“优”。如果输入的成绩超出0~100的范围,则输出“成绩无效”。

public static void main(String[] args) throws Exception {
  System.out.print("请输入成绩:");
  Scanner sc = new Scanner(System.in);
  String str = sc.nextLine();
  try {
   int score = Integer.parseInt(str);
   if (score >= 0 && score < 60){
    System.out.println("不及格");
   } else if (score >= 60 && score < 75){
    System.out.println("及格");
   } else if (score >= 75 && score < 90) {
    System.out.println("良");
   } else if (score >= 90 && score <= 100){
    System.out.println("优");
   } else {
    System.out.println("成绩无效");
   }
  } catch (NumberFormatException e){
   System.out.println("成绩输入错误");
  }
 }

代码还是自己敲的好

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-17
public static void main(String[] args) throws Exception {
System.out.print("请输入成绩:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
try {
int score = Integer.parseInt(str);
if (score >= 0 && score < 60){
System.out.println("不及格");
} else if (score >= 60 && score < 75){
System.out.println("及格");
} else if (score >= 75 && score < 90) {
System.out.println("良");
} else if (score >= 90 && score <= 100){
System.out.println("优");
} else {
System.out.println("成绩无效");
}
} catch (NumberFormatException e){
System.out.println("成绩输入错误");
}
}本回答被提问者采纳
第2个回答  2011-03-17
import java.util.Scanner;

public class ScoreJudge {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
double score = 0;

try {
System.out.print("请输入成绩:");
score = sc.nextDouble();
} catch (NumberFormatException nbFmtExp) {
System.out.println("成绩输入错误");
return;
}

if (score < 0 || score > 100) {
System.out.println("成绩无效");
}

if (score >= 0) {
if (score < 60) {
System.out.println("不及格");
} else if (score < 75) {
System.out.println("及格");
} else if (score < 90) {
System.out.println("良");
} else if (score <= 100) {
System.out.println("优");
}
}

}
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网