关于项目中的try-catch问题
来源:3-4 项目作业
无限精彩
2019-07-05 00:58:36
package com.ceshi.student;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScoreManagement {
int n=0;
int length;
/**
* 显示菜单
*/
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 length
* @return
*/
public float[] initScore(int length) {
float[] f=new float[length];
Scanner sc=new Scanner(System.in);
for(int i=0;i<length;i++) {
System.out.println("请输入第"+(i+1)+"个成绩数据:");
try{
f[i]=sc.nextFloat();
}catch(java.util.InputMismatchException e){
System.out.println("输入的数据格式有误,不能有非数字");
sc.next();
i--;
n++;
}
}
return f;
}
/**
* 显示数据方法
* @param f
*/
public void showDate(float[] f) {
for(int i=0;i<f.length;i++) {
System.out.print(f[i]+" ");
}
}
/**
* 求平均数方法
* @param f
* @return
*/
public float average(float[] f) {
float ave=0;
float a=0;
//for(int i=0;i<f.length;i++)
for(float n:f)
{
a=a+n;
}
ave=a/f.length;
System.out.println("数学平均成绩为:"+ave);
return ave;
}
/**
* 成绩大于85分的人数统计方法
* @param f
* @return
*/
public int numP(float[] f) {
//整型变量nunu,为统计符合条件人数的变量;
int nunu=0;
for(float n:f) {
if(n>85) {
nunu++;
}
}
return nunu;
}
public void update(float[] f, int index, float newScore) {
f[index]=newScore;
}
/**
* 主方法
* @param args
*/
public static void main(String[] args) {
ScoreManagement sco=new ScoreManagement();
Scanner sc=new Scanner(System.in);
int input;
int length;
int index;
float newScore;
float[] f=null;
while(true) {
sco.displayMenu();
System.out.println("请输入对应的数字进行操作:");
try {
input=sc.nextInt();
if(input>5);
System.out.println("输入的数据有误,请重新输入0-5的数字");
}catch(InputMismatchException e){
System.out.println("输入的数据有误");
sc.next();
break;
}
if(input==0) {
System.out.println("退出程序!");
break;
}
switch (input) {
case 1:
System.out.println("请输入要存入数学成绩的数量:");
length=sc.nextInt();
f=sco.initScore(length);
System.out.print("初始化数学成绩:");
sco.showDate(f);
System.out.println();
break;
case 2:
if(f!=null) {
sco.average(f);
}else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 3:
if(f!=null) {
System.out.println("成绩大于85分的人数为:"+sco.numP(f)+"人");
}else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 4:
if(f!=null){ {
System.out.println("修改前:");
System.out.println("成绩为:");
sco.showDate(f);
System.out.println('\n');
System.out.println("请输入要修改数据的位置(从0开始):");
}
try {
index = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能有非数字!");
sc.next();
break;
}
System.out.println("请输入新数据:");
try {
newScore = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("输入的数据格式有误,不能有非数字!");
sc.next();
break;
}
sco.update(f,index,newScore);
System.out.println("修改后:");
System.out.println("成绩为:");
sco.showDate(f);
System.out.println('\n');
}else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
case 5:
if(f!=null) {
System.out.println("成绩为:");
sco.showDate(f);
}else {
System.out.println("还未在数组中插入数据,请重新选择操作!");
}
break;
}
}
}
}
以上代码,问题如下: 问题:请问老师,为什么catch里面的System.out.println("输入的数据有误");不输出 try { input=sc.nextInt(); if(input>5); System.out.println("输入的数据有误,请重新输入0-5的数字"); }catch(InputMismatchException e){ System.out.println("输入的数据有误"); sc.next(); break; }
1回答
好帮手慕小班
2019-07-05
同学你好, 1、catch语句是捕获异常的语句,就是当出现catch中的异常时,就会执行catch中的语句哦,例如
当程序正常执行,没有错误时,不会去执行catch中的语句哦
2、请同学注意if的判断条件后不要加分号,加上分号表示结束这条语句!
3、接收成绩数组的长度时,注意添加try-catch的非数字异常管理哦!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
回答 2
回答 2
回答 1
回答 1
回答 1