
成绩我定义成double了,另外平均分我给你保留两位小数:
public class Student {
private String No; //学号
private String name; //姓名
private String gender; //性别
private int age; //年龄
private double chinese; //语文
private double math; //数学
private double english; //英语
//构造函数
public Student(String no,
String name,
String gender,
int age,
double chinese,
double math,
double english) {
No = no;
thisname = name;
thisgender = gender;
thisage = age;
thischinese = chinese;
thismath = math;
thisenglish = english;
}
@Override
public String toString() {
return "Student{" +
"No='" + No + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
//计算总分
public double getSum() {
return thischinese + thismath + thisenglish;
}
//计算平均分
public double getAverage() {
return (thischinese + thismath + thisenglish) / 3;
}
}
测试类:
class Test {
public static void main(String[] args) {
Student student = new Student(
"20190001",
"张三",
"男",
18,
1095,
890,
1100);
double sum = studentgetSum();
double average = studentgetAverage();
Systemoutprintln("该学生的信息为:" + studenttoString());
Systemoutprintln("该学生的总分为:" + sum);
Systemoutprintln("该学生的平均分为:" + Stringformat("%2f", average));
}
}
运行结果:
这代码会挤在一起,我给你截个图看看:
Student类:
3-28行
28-51行
Test类:
public class Student { //定义一个学生类
private int StuNum; //学号
private int Class; //班级
private char Gender; //性别
private int Age; //年龄
public Student(int StuNum, int Class, char Gender, int Age){//构造函数
thisstuNum = StuNum;
thisclass = Class;
thisgender = Gender;
thisage = Age;
}
public int getStuNum() { //获得学号
return StuNum;
}
public int getClass() { //获得班级号
return Class;
}
public char getGender() { //获得性别
return Gender;
}
public void setGender(char Gender) { //修改性别
thisGender = Gender;
}
public int getAge() { //获得年龄
return Age;
}
public void setAge(int Age) { //修改年龄
thisAge = Age;
}
public class Pupil extends Student //小学生
{
//(由于没说派生后要新加什么东西,所以这里写了省略号,如果想在
// 新派生出来的类里加点什么特殊的东西,直接在省略号位置加就行了
// 下同)
}
public class MidSchoolStu extends Student //中学生
{
//
}
public class UnderGraduate extends Student //大学生
{
//
}
public class PostGraduate extends Student //研究生
{
//
}
public class FreshStudent extends UnderGraduate //一年级学生
{
//
}
public class Sophomore extends UnderGraduate //二年级学生
{
//
}
public class Junior extends UnderGraduate //三年级学生
{
//
}
public class Senior extends UnderGraduate //四年级学生
{
//
}
public class Master extends PostGraduate //硕士生
{
//
}
public class Doctor extends PostGraduate //博士生
{
//
}
给你写完了 很容易的:
/ Catalanjava
Contains 2 methods factorial and cata
catalan calculates the n-th catalan number
factorial calculates the factorial recursively
Receive: n
Return: n-th catalan number
/
public class Catalan {
private long factorial(int n) {
if (n == 0) {
return 1;
} else {
return n factorial(n - 1);
}
}
public long calCatalan(int n) {
return factorial(2 n) / (factorial(n + 1) factorial(n));
}
}
import javautilScanner;
import javautilregexPattern;
/
Calculates the value of a specific Catalan number
Input: integer value
Output: n-th Catalan number
/
public class CatalanDemo {
public static void main(String[] args) {
Scanner in = new Scanner(Systemin);
Catalan catalan = new Catalan();
while (true) {
Systemoutprintln("Which catalan number do you want to calculate (-1 to stop): ");
String s = innextLine();
if (Patternmatches("[0-9]", s)) {
int n = IntegerparseInt(s);
Systemoutprint("n=" + n);
Systemoutprintln(": catalan number is " + catalancalCatalan(n));
} else {
if (Patternmatches("-[0-9]", s)) {
int n = IntegerparseInt(s);
if (n == -1) {
Systemoutprintln("(program exits)");
break;
} else {
Systemoutprintln("Invalid input:\nValid input range is an input >= 0");
}
}
}
}
}
}
以上就是关于Java程序设计全部的内容,包括:Java程序设计、Java程序设计,定义一个表示学生的类Student、程序设计java等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)