Java作业实验九内容二


前言

记录本菜鸡写实验过程出现的问题,各路大神多指教,找出问题直接就评论,我不胜受恩感激。


一.内容

1.具体要求

使用Scanner类实现从键盘输入多位同学的数学、物理、英语三门 课的成绩,要求计算并输出每位同学的总分,各门课的平均成绩及平 均总分。

要求如下(内容二):

1)一次输入一行(格式为姓名 数学成绩 物理成绩 英语成绩)

2)分数之间用空格或逗号隔开

3)用字符串“end”结束输入

4)用nextLine()一次读入一行,用trim()去掉行首和行尾的空行,
然后用split()方法分解字符串,用Double.parseDouble()解析获得
成绩值)

5)平均成绩输出时小数点后只需保留一位小数(String.format())

6)要求添加异常处理

2.我的思路

要求一: 输入格式

1)一次输入一行(格式为姓名 数学成绩 物理成绩 英语成绩)
2)分数之间用空格或逗号隔开
首先得确定输入什么输出什么
例如以下用例:

社会你超姐 100 100 100

我将会将同学是谁,总成绩以及平均成绩输出,个人只写了如果出现除数为零的异常,则报错。也可能是数组下标异常
例如:
在这里插入图片描述
其中的true代表正常循环

要求二:结束字符要求

3)用字符串“end”结束输入
代码如下:
要注意的是java里面的==与C/C++里面的不一样。java是看两个变量的地址是不是相同,而这里的要判断两个字符串相等得有equals();

a: while(f){
    
    
	    	
		  System.out.println(f);
		  Scanner reader=new Scanner(System.in);
	      String str= reader.nextLine();
	      if(str.equals("end")){
    
    
	    	  f=false;
	    	  break a;
	      }

要求三:输入要求

4)用nextLine()一次读入一行,用trim()去掉行首和行尾的空行,
然后用split()方法分解字符串,用Double.parseDouble()解析获得成绩值)
代码如下:

注意sum的初始化
 str.trim();
	      String words[]=str.split(" "); 
	      double sum=0;
	      System.out.println("人:"+":"+words[0]);
	      for(int i=1;i<words.length;i++){
    
    
	          int m=i;	     
	          sum=sum+Double.parseDouble(words[i]);
	          System.out.println("成绩"+m+":"+words[i]);
	       }

要求四:保留小数

5)平均成绩输出时小数点后只需保留一位小数(String.format())
代码如下:

注意这个函数是string类的方法,并且有:String.format("%.Nf")其中N为保留几位小数
	     double sumd=getArith(sum,(words.length-1.0));
	      System.out.println("总成绩成绩:"+":"+sum);
	      System.out.println("平均成绩:"+":"+String.format("%.1f",sumd));

要求五异常处理

1.我的思路

首先得看那个地方可能会出现异常,以我的水平暂时只能看出,Word数组下标和求平均成绩是除数可能为零这两个异常,为图方便我就处理了除数为零的异常

2.选择实现一方法将异常throws

throws的实现
将除法这一方法独立出来运用算数异常类ArithmeticException,返回异常类给catch

public static double getArith(double a, double b)throws ArithmeticException {
    
    
		ArithmeticException ex = new ArithmeticException("对不起,除数为0");

		if (b == 0) {
    
    
		throw ex;

		}

		return a / b;

		}

try…catch…的实现:

try可能出现的异常由catch捕捉
要注意的是e.printStackTrace();返回的是出现异常的位置

 try {
    
    
	     double sumd=getArith(sum,(words.length-1.0));
	      System.out.println("总成绩成绩:"+":"+sum);
	      System.out.println("平均成绩:"+":"+String.format("%.1f",sumd));
	   }catch(ArithmeticException e){
    
    
		  
		   e.printStackTrace();

		   System.out.println("报错,除数不能为0");
	   }

我的代码:

3.我的代码

import java.lang.*;
import java.util.Scanner;

public class ex9_2 {
    
    
	public static double getArith(double a, double b)throws ArithmeticException {
    
    
		ArithmeticException ex = new ArithmeticException("对不起,除数为0");

		if (b == 0) {
    
    
		throw ex;

		}

		return a / b;

		}

	public static void main(String args[]) {
    
    
		boolean f=true;
	a: while(f){
    
    
	    	
		  System.out.println(f);
		  Scanner reader=new Scanner(System.in);
	      String str= reader.nextLine();
	      if(str.equals("end")){
    
    
	    	  f=false;
	    	  break a;
	      }
	      str.trim();
	      String words[]=str.split(" "); 
	      double sum=0;
	      System.out.println("人:"+":"+words[0]);
	      for(int i=1;i<words.length;i++){
    
    
	          int m=i;	     
	          sum=sum+Double.parseDouble(words[i]);
	          System.out.println("成绩"+m+":"+words[i]);
	       }
	      try {
    
    
	     double sumd=getArith(sum,(words.length-1.0));
	      System.out.println("总成绩成绩:"+":"+sum);
	      System.out.println("平均成绩:"+":"+String.format("%.1f",sumd));
	   }catch(ArithmeticException e){
    
    
		  
		   e.printStackTrace();

		   System.out.println("报错,除数不能为0");
	   }
	   
	}
} 
}

总结

各路大神,求个点赞加关注,本渣渣累了

猜你喜欢

转载自blog.csdn.net/weixin_51422230/article/details/121148987