Java computing GPA
•
Java
import java.util.InputMismatchException; import java.util.Scanner; public class Test03 { public static void main(String[] args) { Scanner input = new Scanner(system.in); try { System.out.println("请输入班级总人数:"); int count = input.nextInt(); System.out.println("请输入总成绩:"); int score = input.nextInt(); int avg = score / count; // 获取平均分 System.out.println("本次考试的平均分为:" + avg); } catch (InputMismatchException e1) { System.out.println("输入数值有误!"); } catch (ArithmeticException e2) { System.out.println("输入的总人数不能为0!"); } catch (Exception e3) { e3.printStackTrace(); System.out.println("发生错误!" + e3.getMessage()); } } }
请输入班级总人数: 50 请输入总成绩: 1250a 输入数值有误!
请输入班级总人数: 50a 输入数值有误!
请输入班级总人数: 0 请输入总成绩: 100 输入的总人数不能为0!
请输入班级总人数: 50 请输入总成绩: 1250 本次考试的平均分为:25
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码