java之day5
Posted 哈喽杏红
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之day5相关的知识,希望对你有一定的参考价值。
温故
package day5; import java.util.Scanner; public class Score { //构造函数(不管写不写,都有这个函数,隐藏着) public Score(){ System.out.println("实例化对象调用了我"); } int sum;//和 double avg;//平均分 //一个方法只能return一个值 //求和的方法 下面int是返回值的类型 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; } //求平均分的方法 public double avg(){ //int result = sum(); //return (double)result/3 OR 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 (double)sum/3; } //求和和平均分 用数组 public double[] getAvgAndSum(){ 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; public class ScoreCalc { public static void main(String[] args) { //由类来产生对象(实例化),会调用构造函数 Score score1 = new Score(); //对象生成以后,可以调用对象里的各个属性和方法 int sum = score1.sum(); System.out.println(sum); double avg = score1.avg(); System.out.println(avg); double[] sum1 = score1.getAvgAndSum(); System.out.println(sum1[0]); System.out.println(sum1[1]); } }
==============================================================================================================
package day51; public class Score { //属性 int math; int chinese; int english; int sum; double avg; //构造函数 与类同名,没有返回值,作用:数据的初始化 //与test页面中的实例化对象同时数据初始化相对应 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 day51; import java.util.Scanner; public class ScoreTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入数学成绩"); int math = in.nextInt(); System.out.println("请输入语文成绩"); int chinese = in.nextInt(); System.out.println("请输入英语成绩"); int english = in.nextInt(); // int sum = math +chinese +english; // System.out.println("总成绩为"+sum); // System.out.println("平均成绩"+sum/3.0); // OR下面所有 //单纯的实例化对象 // Score score = new Score(); // score.math = math; // score.chinese = chinese; // score.english = english; // OR可以传参简便化,即下面这一行代码 //实例化对象的同时进行数据的初始化 Score score = new Score(math, chinese, english); double result1 = score.avg(); double result2 = score.sum(); System.out.println("平均成绩为"+result1); System.out.println("总成绩为"+result2); } }
==================================================================================================================
package day52; import java.util.Arrays; import java.util.Scanner; public class shuzu { //2:把方法封装进类 public String[] one(String[] name){ Arrays.sort(name); return name; } }
package day52; import java.io.FilePermission; import java.io.StringBufferInputStream; import java.util.Arrays; import java.util.Scanner; public class shuzuTest{ public static void main(String[] args) { //1:准备数据 System.out.println("请输入5个数"); String[]name = new String[5]; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { name[i] = in.next(); } //3:调用类的方法(先实例化出对象) shuzu one = new shuzu(); String[] two = one.one(name); for (int i = 0; i < 5; i++) { System.out.print(two[i]+" "); } //OR 整成字符串 //System.out.println(Arrays.toString(two)); } }
=================================================================================================================
传对象
package day53; public class Score { int math; int chinese; int english; }
package day53; public class Sum { //传对象 public int sum(Score a){ return (a.math+a.chinese+a.english); } }
package day53; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); Score fen = new Score(); System.out.println("请输入数学成绩"); fen.math = in.nextInt(); System.out.println("请输入语文成绩"); fen.chinese = in.nextInt(); System.out.println("请输入英语成绩"); fen.english = in.nextInt(); Sum he = new Sum(); int sum1 = he.sum(fen); System.out.println(sum1); } }
======================================================================================================================
package day54; public class Student { String name; int age; double height; }
package day54; public class StudentManager { public double heightAvg(Student[] H){ double sum = 0; for (int i = 0;i<5;i++){ sum = sum + H[i].height; } return sum/5; } }
package day54; import java.util.Scanner; public class StudentTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); Student[] stu = new Student[5]; for (int i = 0; i < 5; i++) { System.out.println("输入"+(i+1)+"个身高"); stu[i] = new Student(); stu[i].height = in.nextDouble(); } StudentManager stum = new StudentManager(); double avg = stum.heightAvg(stu); System.out.println(avg); } }
===============================================================================================================
package day55; public class User { String name; String pwd; String sex; int age; String resignDate; String degree; public User() { } public User(String name,String pwd,String sex,int age,String resignDate,String degree){ this.name = name; this.pwd = pwd; this.sex = sex; this.age = age; this.resignDate = resignDate; this.degree = degree; } public void showMessage(){ System.out.println("大家好!我是"+name+",今年"+age+"岁"); System.out.println("我是"+resignDate+"注册的,目前的等级是:"+degree); } }
package day55; import java.util.Scanner; public class UserTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); User user = new User(); System.out.println("请输入你的昵称"); user.name = scanner.next(); System.out.println("请输入你的密码"); user.pwd = scanner.next(); System.out.println("请输入你的性别"); user.sex = scanner.next(); System.out.println("请输入你的年龄"); user.age = scanner.nextInt(); System.out.println("请输入你的注册时间"); user.resignDate = scanner.next(); System.out.println("请输入你的等级"); user.degree = scanner.next(); user.showMessage(); } }
以上是关于java之day5的主要内容,如果未能解决你的问题,请参考以下文章