Java实验八

一、实验目的

  1. 学习应用泛型编写程序
  2. 掌握使用枚举类型编写程序,学会运用for语句简化。

实验内容:

必做
1、 for语句的简化,对象数组,编写程序,满足以下条件:(需讲解)

  • 新建一个学生Student类,属性包括学号、姓名、数学、英语和计算机三门课程成绩及平均分。
  • 新建一个学生班级类StudentClass,包含变量一个学生数组长度为5,代表5名学生Student;
  • 在构造方法中新建5位学生的基本信息,要求可以通过键盘输入中修改学生除学号、平均分之外的信息;
  • 要求该类具有计算学生平均分并最终输出每位学生的学号、姓名、平均分。
  • (尽量用for语句简化形式来做)
    (键盘输入请参考实验二中的testInput.java文件)

实验解答

  1. 创建Student
package ex08;

public class Student {
	String num;//学号
	String name;//姓名
	float Mathematical;//数学
	float English;//英语
	float compurter;//计算机
	float AverageScore = (Mathematical + English + compurter)/3;
	//float AverageScore = (Mathematical + English + compurter)/3;//平均分
	
	//构造方法
	public Student(String num,String name,float Mathematical,float English,float compurter){
		this.num = num;
		this.name = name;
		this.Mathematical = Mathematical;
		this.English = English ;
		this.compurter = compurter ;
		AverageScore = (Mathematical + English + compurter)/3;
		//System.out.println("学号:"+num+" "+"姓名:"+name+" "+"平均分:"+AverageScore );
	}
	/*
	public static void m1() {
		System.out.println(num+" "+name+" "+Mathematical+" " +English+" " +compurter+" " +AverageScore );
		
	}
	*/
}


  1. 创建StudentClass
package ex08;

import java.util.Scanner;

public class StudentClass {

	public static void main(String[] args) {
		Student[] mate = new Student[5];

		//构造方法赋值
		Student zhangsan = new Student("1801","zhangsan",59,58,57);
		Student lisi = new Student("1802","lisi",59,58,57);
		Student wangwu = new Student("1803","wangwu",59,58,57);
		Student maliu = new Student("1804","maliu",59,58,57);
		Student zhaoqi = new Student("1805","zhaoqi",59,58,57);
		
		//zhangsan.AverageScore;
		
		
		//给学生数组赋值
		mate[0] = zhangsan;
		mate[1] = lisi;
		mate[2] = wangwu;
		mate[3] = maliu;
		mate[4] = zhaoqi;
		
		
		
		for (Student i :mate) {
			System.out.println(i.num+" "+i.name+" "+i.Mathematical+" "+i.English+" "+i.compurter+" "+i.AverageScore );
			System.out.println("是否修改,是请输入1,否则输入0");
			Scanner s = new Scanner(System.in);
			boolean c = s.hasNext();
			if(c) {
				System.out.println("姓名:");
				Scanner s1 = new Scanner(System.in);
				i.name = s1.next();

				System.out.println("数学成绩:");
				Scanner s2 = new Scanner(System.in);
				i.Mathematical = s2.nextInt();

				System.out.println("英语成绩:");
				Scanner s3 = new Scanner(System.in);
				i.English  = s3.nextInt();

				System.out.println("计算机成绩:");
				Scanner s4 = new Scanner(System.in);
				i.compurter  = s4.nextInt();

				System.out.println("修改后的信息");
				System.out.println("姓名:" + i.name + "学号:" + i.num + "计算机成绩:" + "平均分:" );
				System.out.println("------------------");
			}else {
				System.out.println("姓名:" + i.name + "学号:" + i.num +  "平均分:" );
				System.out.println("-------------");
			}
			
		}
			

	}
}


实验总结:

学习了数组及对象数组的使用

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值