这个试了半天想不出
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Homework1 {
/**
* 显示菜单方法
*/
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("**************************************");
}
/**
* 显示数组中的元素
*
* @param f 数组名
*/
public void show(float[] f) {
System.out.println("数学成绩为:");
for (float n : f) {
System.out.print(n + " ");
}
System.out.println();
}
/**
* 初始化数学成绩方法 可插入数据,小数,另外数据长度可以由用户自己定义
*
* @author Administrator
*
*/
public float[] initScore() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要存储的数学成绩的数量:");
float[] a = null;
try {
int b = sc.nextInt();
a = new float[b];
for (int i = 0; i < b; i++) {
try {
System.out.println("请输入第" + (i + 1) + "个数据:");
a[i] = sc.nextFloat();
} catch (InputMismatchException e) {
System.out.println("输入格式有误!不能是非数字");
sc.next();
i--;
continue;
}
}
} catch (InputMismatchException e) {
System.out.println("输入格式有误!不能是非数字");
sc.next();
return null;
}
return a;
}
/**
* 求平均成绩方法
*
* @param f 数组名
* @return 平均值返回值
*/
public float average(float[] f) {
float sum = 0, avg;
for (int i = 0; i < f.length; i++) {
sum += f[i];
}
avg = sum / f.length;
System.out.println("数学平均成绩为:" + avg);
return avg;
}
/**
* 统计成绩大于85分的人数的方法
*
* @param f 数组名
* @return sum 人数总和
*/
public int count(float[] f) {
int sum = 0;
for (float a : f) {
if (a >= 85) {
sum++;
}
}
System.out.println("成绩大于85分的人数为:" + sum);
return sum;
}
/**
* 修改指定位置处的成绩
*
* @param f 数组名
* @param index 修改的位置(从0开始)
* @param newScore 新的成绩
*/
public void update(float[] f, int index, float newScore) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的数据的位置(从0开始):");
try {
index = sc.nextInt();
System.out.println("请输入新数据:");
newScore = sc.nextFloat();
} catch (InputMismatchException e) {
System.out.println("输入格式有误!不能是非数字");
sc.next();
return;
}
f[index] = newScore;
}
/**
* 主方法实现
*
* @param args
*/
public static void main(String[] args) {
Homework1 hm = new Homework1();
Scanner sc = new Scanner(System.in);
float[] a = null;
int input = 0, n = 0, k = 0;// n为要修改的位置,k为修改的成绩
while (true) {
hm.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:
// 插入数据
try {
a = hm.initScore();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能有非数字!");
sc.next();
}
if (a != null) {
hm.show(a);
} else {
System.out.println("插入数据失败,请重新选择操作!");
}
break;
case 2:
// 求成绩的平均值
if (a != null) {
hm.average(a);
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 3:
// 统计成绩大于85分的人数
if (a != null) {
hm.count(a);
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 4:
// 修改指定位置的成绩
if (a != null) {
System.out.print("成绩修改前");
hm.show(a);
hm.update(a, n, k);
System.out.print("成绩修改后");
hm.show(a);
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 5:
// 打印输出所有成绩
if (a != null) {
hm.show(a);
} else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
}
}
}
}
修改建议:建议同学可以判断一下用户输入的要修改的位置。使其在0到(数组长度-1)的范围内。避免出现数组索引越界异常
老师,修改成绩下标越界这里我没试出来,还有为什么要添加return啊,该怎么判别trycatch模块后面何时需要添加返回值啊
15
收起
正在回答
2回答
同学你好,同学可以判断一下用户输入的要修改的位置。使其在0到(数组长度-1)的范围内。如下图所示:

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星