定义一个student类,其中有成员变量,学号,姓名,性别,是否为班干部,以及语文数学英语的分数,求成绩的方法。
定义一个主类,主方法中通过学生类创造对象,通过键盘输入学生的属性值,然后输出学生的属性值,调用方法输出该学生的总成绩和平均成绩。

JAVA类与对象 定义一个student类
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注
import java.util.Scanner; public class HelpOther { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入学生学号:"); String id=scanner.nextLine(); System.out.println("请输入学生姓名:"); String name=scanner.nextLine(); System.out.println("请输入学生性别(1:男生 0:女生):"); String isMan=scanner.nextLine(); boolean man=false; if(isMan.equals("1")){ man=true; } System.out.println("请输入学生是否领导(1:是 0:不是)"); String isLeader=scanner.nextLine(); boolean leader=false; if(isLeader.equals("1")){ leader=true; } System.out.println("请输入学生数学分数"); float mathScore=scanner.nextFloat(); System.out.println("请输入学生语文分数"); float chineseScore=scanner.nextFloat(); Student student=new Student(id,name,man,leader,mathScore,chineseScore); System.out.println(student.avScore); } } class Student { String id; //学号 String name;//姓名 boolean isMan;//是否男生 boolean isLeader;//是否领导 float mathScore;//数学分数 float chineseScore;//语文分数 float avScore;//平均成绩 public Student(String id ,String name,boolean isMan, boolean isLeader,float mathScore,float chineseScore){ this.id=id; this.name=name; this.isMan=isMan; this.isLeader=isLeader; this.mathScore=mathScore; this.chineseScore=chineseScore; //计算平均分数 this.avScore=(mathScore+chineseScore)/2; } }
如有帮助望采纳哈哈。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1