请问这个代码可以吗,有什么看需要改的或者改进的地方吗

请问这个代码可以吗,有什么看需要改的或者改进的地方吗

package imooc.homework;


import java.util.Scanner;

import java.util.InputMismatchException;

public class StuScore {

/**

 * 显示菜单的方法

 */

public void dispaly() {

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("***************************************");

}

/**

 * 初始化数学成绩

 * @return 返回数组

 */

public float[] intinScore() {

int n=0;

Scanner sc=new Scanner(System.in);

while(true) {

System.out.println("请输入要存储的数学成绩的数量:");

try {

n=sc.nextInt();

}catch(InputMismatchException e){

System.out.println("输入的数据格式有误,不能有非数字!");

sc.next();

continue;

}

break;

}

float[] a=new float[n];

for(int i=0;i<n;i++) {

System.out.println("请输入第"+(i+1)+"个数据");

try {

a[i]=sc.nextFloat();

}catch(InputMismatchException e) {

System.out.println("输入的数据格式有误,不能有非数字!");

sc.next();

i--;

continue;

}

}

return a;

}

/**

 * 求平均成绩

 * @param f 数组

 * @return 返回平均成绩

 */

public float average(float[] f) {

float avg=0,sum=0;

for(int i=0;i<f.length;i++) {

sum=sum+f[i];

}

avg=sum/f.length;

return avg;

}

/**

 * 统计成绩大于85分的人数

 * @param f

 * @return 大于85分的数量

 */

public int count(float[] f) {

int count=0;

for(float n:f) {

if(n>85) {

count++;

}

}

return count;

}

/**

 * 修改指定位置的成绩

 * @param f 传入的数组

 * @param index 位置(从0开始)

 * @param newScore 新的成绩

 */

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) {

StuScore ss=new StuScore();

Scanner sc=new Scanner(System.in);

int input=0;

float[] f=null;

while(true) {

ss.dispaly();

System.out.println("请输入对应的数字进行操作!");

try {

input=sc.nextInt();

}catch(InputMismatchException e) {

System.out.println("输入的格式有误,不能有非数字!");

sc.hasNext();

continue;

}

if(input==0) {

System.out.println("退出程序!");

break;

}else {

switch(input) {

case 1:

f=ss.intinScore();

break;

case 2:

if(f!=null) {

System.out.println("数学平均成绩为:"+ss.average(f));

}else {

System.out.println("还未在数组中插入数据,请重新选择操作!");

}

break;

case 3:

if(f!=null) {

System.out.println("成绩大于85分的人数为:"+ss.count(f));

}else {

System.out.println("还未在数组中插入数据,请重新选择操作!");

}

break;

case 4:

int index=0;

float newScore=0;

System.out.println("修改前:");

ss.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;

}

ss.update(f, index, newScore);

System.out.println("修改后:");

ss.displayAllScore(f);

break;

case 5:

ss.displayAllScore(f);

break;

}

}

}

}


}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕阿莹 2019-08-12 18:10:36

同学你好,非常感谢yuhuahuaL同学的建议,这里确实可以更好的优化一下。

http://img1.sycdn.imooc.com//climg/5d513af1000133fa06620393.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

吃吃吃鱼的猫 2019-07-25 10:06:01

同学你好,楼上同学的回复很赞哦~

可以参考一下呢~

祝:学习愉快~

BraveSong灬 2019-07-25 03:37:02

package com.wfc;

import java.util.Scanner;

public class TestMath {
 // 定义显示单的方法
 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);
  System.out.println("请输入要储存的数学成绩的个数(1-10)");
  int a = 0;
  try {
   a = sc.nextInt();
  } catch (Exception e) {
   sc.next();
   while (a > 10 || a == 0) {// 循环控制输入成绩个数为1--10
    System.out.println("请重新输入储存的数学成绩的个数(1--10)");
    try {
     a = sc.nextInt();
    } catch (Exception e1) {
     sc.next();
     continue;
    }
   }
  }
  float[] math = new float[a];
  for (int i = 0; i < math.length; i++) {
   System.out.println("请输入第" + (i + 1) + "个数据(0-100)");
   try {
    math[i] = sc.nextFloat();
    if (math[i] > 100) {
     System.out.println("输入有误请,重新输入");
     i--;
    }
   } catch (Exception e) {
    sc.next();
    System.out.println("输入有误请,重新输入");
    i--;
   }
  }
  System.out.println("您输入的成绩为:");
  for (float n : math) {
   System.out.print(n + "  ");
  }
  System.out.println();
  return math;
 }
 // 定义求平均成绩的方法
 public void average(float[] math) {
  float a = 0; // 定义变量a用于保存平均成绩
  float sum = 0; // 定义变量sum用于保存总成绩
  if (math == null) {
   System.out.println("您还没有初始化数学成绩,请先初始化数学成绩");
  } else {
   for (int i = 0; i < math.length; i++) {
    sum += math[i];
   }
   a = sum / math.length;
   System.out.println("平均成绩为:" + a);
  }
 }
 // 定义统计成绩大于85分的人数的方法
 public void count(float[] math) {
  int a = 0; // 定义变量a保存分数大于85分的人数
  if (math == null) {
   System.out.println("您还没有初始化数学成绩,请先初始化数学成绩");
  } else {
   for (int i = 0; i < math.length; i++) {
    if (math[i] > 85)
     a++;
   }
   System.out.println("分数大于85分的人数有:" + a + "人");
  }
 }
 // 定义修改指定位置处成绩的方法
 public void update(float[] math) {
  if (math == null) {
   System.out.println("您还没有初始化数学成绩,请先初始化数学成绩");
  } else {
   Scanner sc = new Scanner(System.in);
   System.out.println("请输入要修改的位置1--" + math.length);
   int index = 0;
   try {
    index = sc.nextInt();
   } catch (Exception e) {
    sc.next();
   }
   while (index > math.length || index == 0) {
    System.out.println("请重新输入要修改的位置1--" + math.length);
    try {
     index = sc.nextInt();
    } catch (Exception e) {
     sc.next();
    }
   }
   System.out.println("请输入新的数据(0-100)");
   float f = -1;
   try {
    f = sc.nextFloat();
   } catch (Exception e) {
    sc.next();
   }
   while (f > 100 || f == 0) {
    System.out.println("请重新输入成绩(0--100)");
    try {
     f = sc.nextFloat();
    } catch (Exception e) {
     sc.next();
    }
   }
   math[index - 1] = f;
  }
 }

 // 定义打印成绩的方法
 public void print(float[] math) {
  if (math == null) {
   System.out.println("您还没有初始化数学成绩,请先初始化数学成绩");
  } else {
   System.out.print("成绩为:");
   for (float n : math) {
    System.out.print(n + "  ");
   }
   System.out.println();
  }
 }

 public static void main(String[] args) {
  float[] b = null;
  TestMath a = new TestMath();
  Scanner sc = new Scanner(System.in);
  while (true) {
   a.displayMenu();
   System.out.println("请输入对应的数字进行操作:");
   int a1 = 0;
   try {
    a1 = sc.nextInt();
   } catch (Exception e1) {
    sc.next();
    System.out.println("输入错误,请重新输入");
    continue;
   }
   if (a1 == 0) {
    System.out.println("程序退出");
    break;
   }
   switch (a1) {
   case 1:
    b = a.initScore();
    break;
   case 2:
    a.average(b);
    break;
   case 3:
    a.count(b);
    break;
   case 4:
    System.out.println("修改前");
    a.print(b);
    a.update(b);
    System.out.println("修改后");
    a.print(b);
    break;
   case 5:
    a.print(b);
    break;
   default:
    System.out.println("输入有误,请重新输入");
    break;
   }
  }
 }
}

我这样更完善点给你参考下

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师