程序不能正常运行,提示case2~case4是Dead code,老师帮忙看一下怎么改
package com.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScoreManage {
public void displayMenu() {
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("***********************************");
}
public float[] initScore() {
Scanner sc = new Scanner(System.in);
int len=0;
System.out.println("请输入要存储的数学成绩的数量:");
try {
len= sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能为非数字!");
sc.next();
}
float[] f = new float[len];
for (int i= 0; i< len; i++) {
System.out.println("请输入第" + (i + 1) + "个数据:");
try {
f[i] = sc.nextFloat();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能为非数字!");
sc.next();
i--;
}
}
sc.close();
return f;
}
public float average(float[] f) {
float sum = 0;
for (int i = 0; i < f.length; i++) {
sum += f[i];
}
float avg = sum / f.length;
return avg;
}
public int count(float[] f) {
int count = 0;
for (float n : f) {
if (n > 85) {
count++;
}
}
return count;
}
public void update(float[] f, int index, float newScore) {
f[index] = newScore;
}
public void displayAllScore(float[] f) {
System.out.println("成绩为:");
for (float n : f) {
System.out.print(n + " ");
}
System.out.println();
}
public static void main(String[] args) {
ScoreManage sm = new ScoreManage();
Scanner sc = new Scanner(System.in);
int input = 0, index = 0;
float[] f = null;
float newScore = 0f;
while (true) {
sm.displayMenu();
System.out.println("请输入对应的数字进行操作:");
try {
input = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能有非数字!");
sc.next();
continue;
}
if (input == 0) {
System.out.println("退出程序!");
break;
}
}
switch (input) {
case 1:
f = sm.initScore();
break;
case 2:
if (f != null) {
System.out.println("数学平均成绩为;" + sm.average(f));
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 3:
if (f != null) {
System.out.println("成绩大于85分的人数为:" + sm.count(f));
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 4:
if (f != null) {
System.out.println("修改前:");
sm.displayAllScore(f);
System.out.println("请输入要修改数据的位置(从0开始):");
try{
index = sc.nextInt();
System.out.println("请输入新数据:");
newScore = sc.nextFloat();
}catch(InputMismatchException e) {
System.out.println("输入的数据格式有误,不能有非数字!");
sc.next();
break;
}
sm.update(f, index, newScore);
System.out.println("修改后:");
sm.displayAllScore(f);
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 5:
sm.displayAllScore(f);
break;
default:
System.out.println("输入的数据格式有误,不能是0~5以外的数字!");
}
sc.close();
}
}1
收起
正在回答
1回答
同学你好,1、出现Dead code问题的原因是,同学没有将switch-case的方法放在while循环中,input的值一直是0,所以后面的case2、3、4都不会执行,所以编辑器会报出Dead code哦,修改建议如下:

2、建议在initScore方法中的sc.close()方法去掉,因为在一段程序中,使用close方法就会关闭Scanner,导致在下面的Scanner不能使用哦!所以这里建议去掉initScore方法中的close方法!

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星