2.1语法
public 返回值类型 方法名(){
//方法体
}
2.2方法的调用语法
对象名.方法名
计算平均分和总成绩
public class Score {
/**
* 创建类 ScoreCalc 编写方法实现各功能 编写测试类
* 从键盘接收三门课分数,(java c# db)
* 计算三门课的平均分和总成绩,
* 编写成绩计算类实现功能 _______
*/
int java;
int c;
int db; public double avg() {
double a = (double) (java + c + db) / 3;
return a;
} public int addAll() { int ad = java + c + db;
return ad;
} }
Score
import java.util.Scanner; public class TestScore {
public static void main(String[] args) {
Score sc=new Score();
Scanner input=new Scanner(System.in);
System.out.println("请输入java成绩:");
sc.java=input.nextInt();
System.out.println("请输入c成绩:");
sc.c=input.nextInt();
System.out.println("请输入db成绩:");
sc.db=input.nextInt(); System.out.println(sc.addAll());//总成绩
System.out.println(sc.avg());//平均成绩
} }
TestScore
package week2; public class Manager {
String name;
String password;
/**
* 打印学员信息
*/ public void show() {
System.out.println("管理员名为:"+name+"\n密码为:"+password); } }
package week2; public class TestManager {
public static void main(String[] args) {
Manager manager=new Manager();
manager.name="王铎";
manager.password="123";
manager.show();
} }
TestManager
2.3用数组作为参数
package week2;
public class JavaScore {
public double avg(int[] scores) {
int sum = 0;
double avg1 = 0.0;
for (int i = 0; i < scores.length; i++) {
sum = sum + scores[i]; }
avg1 = (double) sum / scores.length; return avg1; }
public int max(int[] scores) {
int max1 = scores[0];
for (int i = 0; i < scores.length; i++) {
if (max1 < scores[i]) {// 最大值
max1 = scores[i]; }
}
return max1; } }
package week2; import java.util.Scanner; public class TestScore {
public static void main(String[] args) {
JavaScore javascore=new JavaScore();
int []scores=new int [4];
Scanner input=new Scanner(System.in);
System.out.println("请输入5名参赛者的平均成绩:");
for (int i = 0; i < scores.length; i++) {
scores[i]=input.nextInt(); }
//pingj
javascore.avg(scores); } }
2.4多个参数的方法
package week2; public class StudentBz {
String[] names = new String[30]; // 添加学生信息
public void addName(String name) {
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
names[i] = name;
break; } } } // 展示学生信息
public void nameShow() {
for (int i = 0; i < names.length; i++) {
if (names[i] != null) {
System.out.print(names[i] + ","); } } } public boolean searchFind(int start, int end, String name) {
boolean find = false;// 标识查找的位置
for (int i = start - 1; i < end; i++) {// i=start-1sss数组下标
if (names[i].equals(name)) {
find = true;
break; } } return find;
} public boolean updateName(String newName, String oldName) {
boolean find = false;
// 查找老name
for (int i = 0; i < names.length; i++) {
if (oldName.equals(names[i])) {
names[i] = newName;
find = true;
break; } }
return find; } }
package week2; import java.util.Scanner; public class TestAdd {
public static void main(String[] args) {
StudentBz stu=new StudentBz();
for (int i = 0; i < 3; i++) {
Scanner input=new Scanner(System.in);
System.out.print("请输入学生姓名:");
String na=input.next();
stu.addName(na); } stu.nameShow(); boolean result=stu.searchFind(2, 5, "name2");
System.out.println(result); boolean updateResult=stu.updateName("newName", "name2");
System.out.println("是否修改成功"+updateResult);
stu.nameShow();
} }
2.5对象作为参数
package week2; public class Student {
String name;
int age;
int no;
int score; }
Student
package week2; public class StudentBz2 { Student students[] = new Student[30]; public void addStudent(Student student) {
for (int i = 0; i < students.length; i++) {
if (students[i] == null) {
students[i] = student;
break;
} } } public void showStudent() {
for (int i = 0; i < students.length; i++) {
if (students[i] != null) {
System.out.println(students[i].name + "," + students[i].age
+ "," + students[i].no + "," + students[i].score + ","); } } } }
StudentBz2
package week2; import java.util.Scanner; public class TestStudentBz2 {
public static void main(String[] args) {
StudentBz2 stubz=new StudentBz2();
Scanner input=new Scanner(System.in);
for (int i = 0; i < 2; i++) {
Student stu=new Student();//循环几次出现几个对象
System.out.print("请输入姓名:");
stu.name=input.next();
System.out.print("请输入年龄:");
stu.age=input.nextInt();
System.out.print("请输入学号:");
stu.no=input.nextInt();
System.out.print("请输入成绩:");
stu.score=input.nextInt();
stubz.addStudent(stu);
}
System.out.println(); stubz.showStudent(); } }
TestStudentBz2
练习实现对客户姓名的排序
package week2; import java.util.Arrays; public class KH { public void sortName(String []names) {
Arrays.sort(names); }
}
KH
package week2; public class KHTest {
public static void main(String[] args) {
String []names={"j","sds","sdssf","ssf"};
KH kh=new KH();
System.out.println("排序前");
for (int i = 0; i < names.length; i++) {
if (names[i]!=null) {
System.out.println(names[i]+","); } }
kh.sortName(names);
System.out.println("排序后");
for (int i = 0; i < names.length; i++) {
if (names[i]!=null) {
System.out.println(names[i]+","); } }
} }
KHTest
带参数练习
package week2; public class CustomerBiz {
String []customer=new String[30]; public void addName(String name) {
for (int i = 0; i < customer.length; i++) {
if (customer[i]==null) {
customer[i]=name;
break; } } } public void showNames() {
System.out.println("*****************");
System.out.println("客户姓名列表"); for (int i = 0; i < customer.length; i++) {
if (customer[i]!=null) {
System.out.println(customer[i]+","); } } } }
CustomerBiz
package week2; import java.util.Scanner; public class TestCustomer {
public static void main(String[] args) {
boolean con = false;
CustomerBiz cust = new CustomerBiz();
Scanner input = new Scanner(System.in); for (int i = 0; i < 3; i++) {
System.out.print("请输入客户姓名:");
String na = input.next();
cust.addName(na);
System.out.println("是否继续(y/n)");
String n = input.next(); if (!n.equals("y")) { } }
cust.showNames(); } }
TestCustomer