try-catch的几个问题
package com.imooc;
import java.util.Scanner;
public class ScoreList {
private int num;
private float[] scoreList;
//成绩单管理系统私有属性:学生人数num及成绩单数组scoreList
public int getNum() {
return num;
}
public float[] getScoreList() {
return scoreList;
}
public void setScoreList(float[] scoreList) {
this.scoreList = scoreList;
}
public void setNum(int num) {
this.num = num;
}
public ScoreList(int num, float[] scoreList) {
super();
this.num = num;
this.scoreList = scoreList;
}
//生成ScoreList的有参/无参构造以及参数的get/set方法
public ScoreList() {
super();
// TODO 自动生成的构造函数存根
}
//求数组平均值
public float averScore() {
float sum = 0;
for(int i=0; i<scoreList.length; i++) {
sum+=scoreList[i];
}
float averScore=sum/scoreList.length;
return averScore;
}
//统计85以上的人数
public int highScoreNum() {
int num = 0;
for(int i=0; i<scoreList.length; i++) {
if(scoreList[i]>85) {
num++;
}
}
return num;
}
//更新第(i+1)个数据
public void updateScore(int i, float score) {
scoreList[i] = score;
}
//显示所有成绩
public void display() {
System.out.println("成绩为:");
for(int i = 0; i<scoreList.length; i++) {
System.out.print(scoreList[i]+" ");
}
}
//显示操作菜单
public void menu() {
System.out.println("***************************************");
System.out.println(" 1--初始化数学成绩");
System.out.println(" 2--求成绩平均值");
System.out.println(" 3--统计成绩大于85分的人数");
System.out.println(" 4--修改指定位置处的成绩");
System.out.println(" 5--打印输出所有成绩");
System.out.println(" 0--退出");
System.out.println("***************************************");
System.out.println("请输入对应数字进行操作:");
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
float[] scoreList;
int num;
int choice;
ScoreList list = new ScoreList();
Scanner sc = new Scanner(System.in);
//do-while循环展示操作菜单,完成循环操作
do {
System.out.println();
System.out.println();
list.menu();
try {
choice = sc.nextInt();
if(choice==0) {
System.out.println("退出程序!");
return;
//输入0时退出程序
}
} catch (Exception e) {
System.out.println("输入有误,请重新输入!");
sc.next();
continue;
//输入格式错误时报错并进行下次循环
}
switch(choice){
case 1:
System.out.println("请输入要存储的数学成绩的数量:");
try {
num=sc.nextInt();
while(num<1) {
System.out.println("输入范围错误,请重新输入!");
num=sc.nextInt();
}
//数组长度小于1时循环输入
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
continue;
}
scoreList=new float[num];
for(int i = 0 ; i<num ; i++) {
System.out.println("请输入第"+(i+1)+"个数据:");
try {
scoreList[i]=sc.nextInt();
if(scoreList[i]<0 || scoreList[i]>100) {
System.out.println("输入范围错误,请重新输入!");
i--;
}
//成绩小于0或大于100时重新输入
} catch (Exception e) {
System.out.println("输入错误,请重试!");
continue;
}
}
System.out.println("初始化完成!");
list.setScoreList(scoreList);
break;
case 2:
if(list.getScoreList()==null) {
System.out.println("请先初始化成绩单!");
continue;
}
System.out.println("平均成绩为:"+list.averScore());
break;
case 3:
if(list.getScoreList()==null) {
System.out.println("请先初始化成绩单!");
continue;
}
System.out.println("85分以上的人数为:"+list.highScoreNum());
break;
case 4:
if(list.getScoreList()==null) {
System.out.println("请先初始化成绩单!");
continue;
}
System.out.println("修改前:");
list.display();
System.out.println("请输入要修改的数据位置(从0开始):");
int i;
try {
i = sc.nextInt();
} catch (Exception e) {
System.out.println("输入错误,请重试!");
continue;
}
System.out.println("请输入新数据:");
float score;
try {
score = sc.nextFloat();
while(score<0 || score>100) {
System.out.println("输入范围错误,请重新输入!");
score = sc.nextFloat();
}
} catch (Exception e) {
System.out.println("输入错误,请重试!");
continue;
}
try {
list.updateScore(i, score);
} catch (Exception e) {
System.out.println("修改数据不存在!");
continue;
}
System.out.println("修改后:");
list.display();
break;
case 5:
if(list.getScoreList()==null) {
System.out.println("请先初始化成绩单!");
continue;
}
list.display();
break;
default:
System.out.println("无效命令,请重新输入!");
}
}while(true);
}
}在catch中使用continue会直接跳回一级菜单开始下一次循环,如何才能只跳回上一级菜单呢?比如成绩输错了只重新输入成绩而非从头开始。
如果不在一开始用if(choice==0)来判断程序结束,而是在do-while中用while(choice!=0)来判断就会报错,主方法中的全局变量被try-catch包裹后就无法作用于while判断条件了是为什么呢?
sc的控制台输入应该在什么时候关闭呢?do-while结束后写sc.close()会报错
正在回答
同学你好,
1、不循环的情况下返回上一级,这部分在这个作业中是无法实现的哦~同学可以不用纠结个功能哦~只需要实现提示信息并重新输入的操作就可以了~
2、try-catch语句对变量值作用范围:
在语句块内部定义的变量,作用域在语句块内部,外部不可见。
在语句块外部定义的变量,在语句块内部可以对变量进行修改。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
同学你好,作业完成的不错,
1、如果想要重新输入成绩,应该使用如下思路:
输入有误之后,我们应该让用户重新输入本次的成绩,所以这里我们可以使用i--;并跳过此次循环,进行下次循环。使其仍为本次数据赋值,修改后的代码如下图所示:

2、 main方法中的变量都是局部变量,并不是全局变量,我们需要为它进行初始化。只有定义在类中的才是全局变量。
while(choice!=0)会报错的原因是, while(choice!=0)会报错是因为按照程序的执行顺序,首先进行whlie语句的判断,而choice并未给它赋值,所以会报错。而while(true)不会报错,是因为在图片的位置处进行了初始化赋值,如图:

3、sc在这个作业中不需要关闭哦~报错的原因是:因为一旦调用close()方法,所有的Scanner对象的入口都被关闭了,这个对象比较特殊
另外建议同学将作业提交!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星