Java学习记录(补充二:面对对象基础复习,习题)
Posted 天上一片红
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学习记录(补充二:面对对象基础复习,习题)相关的知识,希望对你有一定的参考价值。
求3(可调整)个学员成绩的和或平均分或两者都具
package day5; import java.util.Scanner; public class Score { //构造函数 public Score(){ System.out.println("实例化对象调用了我"); } int sum; double avg; // 定义求和的方法 public int sum() { Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入第" + (i + 1) + "个学员成绩"); int score = scanner.nextInt(); sum += score; } return sum; } //求平均分的方法 凡是方法名报错都是没有返回值return; public double avg(){ double result = sum()/3; return result; } //既求和又求平均分的方法 public double[] getSumAndAvg(){ double[] results = new double[2]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入第" + (i + 1) + "个学员成绩"); int score = scanner.nextInt(); sum += score; } results[0] = sum; results[1] = sum/3; return results; } }
package day5; import java.util.Arrays; public class ScoreCalc { public static void main(String[] args) { // 由类来产生对象 (实例化)会调用你的构造函数 Score score1 = new Score(); // 对象生成以后, 可以调用对象里的各个属性和方法 System.out.println(score1.sum()); System.out.println(Arrays.toString(score1.getSumAndAvg())); } }
结果图:
求三门课的和及平均分(构造有参函数)
package day5_1; public class Score { // 属性 int math;// 数学成绩 int chinese;// 语文成绩 int english;// 英语成绩 int sum;// 总分 double avg;// 平均分 // 构造函数 与类同名,没有返回值 作用:数据的初始化!!! public Score(int math, int chinese, int english) { this.math = math; this.chinese = chinese; this.english = english; } // 方法 // 求和 public int sum() { sum = math + chinese + english; return sum; } // 求平均分 public double avg() { avg = (math + chinese + english) / 3.0; return avg; } }
package day5_1; import java.util.Scanner; public class ScoreTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入数学成绩"); int math = scanner.nextInt(); System.out.println("请输入语文成绩"); int chinese = scanner.nextInt(); System.out.println("请输入英语成绩"); int english = scanner.nextInt(); //单纯的实例化对象 // Score score = new Score(); // score.math = math; // score.chinese = chinese; // score.english = english; //实例化兑现过的同时进行数据的初始化 Score score = new Score(math,chinese,english); int result1 = score.sum(); double result2 = score.avg(); System.out.println(result1); System.out.println(result2); } }
结果图:
学生名字排序(创建一个数组对象)
package day5_2; import java.util.Arrays; public class NameSort { //2:把方法封装 public String[] sort(String[] unOrderArr) { Arrays.sort(unOrderArr); return unOrderArr; } }
package day5_2; import java.util.Arrays; import java.util.Scanner; public class NameSortTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //1:准备数据 String names[]= new String[5]; for (int i = 0; i < 5; i++) { System.out.println("请一次输入第" + (i + 1) + "个学生名字"); names[i] = scanner.next(); } //3:调用类的方法(先实例化出对象) NameSort sortName = new NameSort(); String[] orderArr = sortName.sort(names); System.out.println(Arrays.toString(orderArr)); } }
结果图:
单个对象的三门成绩求均分(传输对象)
package day5_2; public class Student { int math; int chinese; int english; public Student(int math,int chinese,int english){ this.math = math; this.chinese = chinese; this.english = english; } }
package day5_2;
public class StudentCalc {
//创建一个对象(Student)
public double avg(Student student){
int mathScore = student.math;
int chineseScore = student.chinese;
int englishScore = student.english;
double avg = (mathScore+chineseScore+englishScore)/3;
return avg;
}
}
package day5_2;
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数学成绩");
int math = scanner.nextInt();
System.out.println("请输入语文成绩");
int chinese = scanner.nextInt();
System.out.println("请输入英语成绩");
int english = scanner.nextInt();
Student zhangSan = new Student(math, chinese, english);
StudentCalc calc = new StudentCalc();
System.out.println("平均分:"+calc.avg(zhangSan));
}
}
结果图:
多个对象的姓名,年龄,身高,总身高平均(传输对象数组)
package day5_3;
(构造一个有参函数) public class Student { String name; int age; int height; public Student(String name,int age,int height){ this.name = name; this.age = age; this.height = height; } }
package day5_3; public class StudentManager { //创建一个对象数组(Student[]) public int avg(Student[] students) { int sum = 0; for (int i = 0; i < students.length; i++) { sum += students[i].height; } return sum/students.length; } }
package day5_3; import java.util.Scanner; public class StudentManagerTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Student[] students = new Student[2]; for (int i = 0; i < students.length; i++) { System.out.println("输入姓名"); String name = scanner.next(); System.out.println("请输入年龄"); int age = scanner.nextInt(); System.out.println("请输入身高"); int height = scanner.nextInt(); Student student = new Student(name, age, height); students[i] = student; } StudentManager calc = new StudentManager(); System.out.println(calc.avg(students)); } }
结果图:
一次传递多个参数
package Day5_4;
public class User { String nick; String pwd; int age; String sex; String resignDate; String degree; public User(String nick,String pwd,int age,String sex,String resignDate,String degree){ this.nick =nick; this.pwd = pwd; this.age = age; this.resignDate = resignDate; this.sex = sex; this.degree = degree; } public void showMessage() { System.err.println("大家好!我是"+nick+",今年"+age+"岁"); System.err.println("我是"+resignDate+"年注册的,目前等级是:"+degree); } }
package Day5_4; import java.util.Scanner; public class UserTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入您的昵称:"); String nick = scanner.next(); System.out.println("请输入您的密码:"); String pwd = scanner.next(); System.out.println("请输入您的性别:"); String sex = scanner.next(); System.out.println("请输入您的等级:"); String degree = scanner.next(); System.out.println("请输入您的年龄:"); int age = scanner.nextInt(); System.out.println("请输入您的注册时间:"); String resignDate = scanner.next(); User user = new User(nick, pwd, age, sex, resignDate, degree); user.showMessage(); } }
结果图:
以上是关于Java学习记录(补充二:面对对象基础复习,习题)的主要内容,如果未能解决你的问题,请参考以下文章