JAVA37. 判断学生成绩
描述
定义一个方法用于录入学生的考试成绩,要求考试成绩必须在0-100之间,不满足就产生一个自定义异常,控制台输出一个错误信息"分数不合法"(请输出自定义异常对象的错误信息,将错误信息设置为分数不合法)输入描述
控制台输入的int类型整数输出描述
若分数合法则输出该分数,否则输出错误信息分数不合法示例1
输入:
100
输出:
100
示例2
输入:
-1
输出:
分数不合法
Java 解法, 执行用时: 14ms, 内存消耗: 4200KB, 提交时间: 2022-05-28
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); //write your code here...... try{ if(score < 0 || score > 100){ throw new ScoreException("分数不合法"); }else{ System.out.println(score); } }catch(ScoreException e){ System.out.println(e.getMessage()); } } } class ScoreException extends Exception { //write your code here...... public ScoreException(String message){ super(message); } }
Java 解法, 执行用时: 25ms, 内存消耗: 10512KB, 提交时间: 2022-02-09
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); boolean flag = true; //write your code here...... if(!(score >= 0 && score <= 100)) { try { throw new ScoreException("分数不合法"); }catch (ScoreException e) { System.out.println(e.getMessage()); flag = false; } } if(flag) { System.out.println(score); } } } class ScoreException extends Exception { //write your code here...... public ScoreException() { } public ScoreException(String message) { super(message); } }
Java 解法, 执行用时: 26ms, 内存消耗: 10776KB, 提交时间: 2022-02-09
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); //write your code here...... try{ if(score >= 0 && score <= 100) System.out.println(score); else throw new ScoreException("分数不合法"); }catch(ScoreException e){ System.out.println(e.getMessage()); } } } class ScoreException extends Exception { //write your code here...... public ScoreException(String message){ super(message); } }
Java 解法, 执行用时: 27ms, 内存消耗: 10736KB, 提交时间: 2022-02-08
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); //write your code here...... try{ if((score>=0) && (score<=100)){ System.out.println(score); }else{ throw new ScoreException("分数不合法"); } }catch(ScoreException str){ System.out.println(str.getMessage()); } } } class ScoreException extends Exception { //write your code here...... public ScoreException(String message){ super(message); } }
Java 解法, 执行用时: 28ms, 内存消耗: 10500KB, 提交时间: 2022-02-08
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); //write your code here...... try { if (score < 0 || score > 100) { throw new ScoreException("分数不合法"); } System.out.println(score); } catch (ScoreException e) { System.out.println(e.getMessage()); } } } class ScoreException extends Exception { //write your code here...... public ScoreException(String message) { super(message); } }