我的java之路week2类的无参、带参方法

2018-06-18 03:28:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

2.1语法

public 返回值类型 方法名(){

//方法体

}

2.2方法的调用语法

对象名.方法名

计算平均分和总成绩

 1 public class Score {
 2     /**
 3      * 创建类 ScoreCalc 编写方法实现各功能 编写测试类
 4      *  从键盘接收三门课分数,(java c# db) 
 5      *  计算三门课的平均分和总成绩,
 6      * 编写成绩计算类实现功能 _______
 7      */
 8     int java;
 9     int c;
10     int db;
11 
12     public double avg() {
13         double a = (double) (java + c + db) / 3;
14         return a;
15     }
16 
17     public int addAll() {
18 
19         int ad = java + c + db;
20         return ad;
21     }
22 
23 }
Score
 1 import java.util.Scanner;
 2 
 3 
 4 public class TestScore {
 5     public static void main(String[] args) {
 6         Score sc=new Score();
 7         Scanner input=new Scanner(System.in);
 8         System.out.println("请输入java成绩:");
 9         sc.java=input.nextInt();
10         System.out.println("请输入c成绩:");
11         sc.c=input.nextInt();
12         System.out.println("请输入db成绩:");
13         sc.db=input.nextInt();
14         
15         System.out.println(sc.addAll());//总成绩
16         System.out.println(sc.avg());//平均成绩
17     }
18 
19 }
TestScore
package week2;

public class Manager {
	String name;
	String password;
	/**
	 * 打印学员信息
	 */
	
	public void show() {
		System.out.println("管理员名为:"+name+"\n密码为:"+password);
		
	}

}

  

 1 package week2;
 2 
 3 public class TestManager {
 4     public static void main(String[] args) {
 5         Manager manager=new Manager();
 6         manager.name="王铎";
 7         manager.password="123";
 8         manager.show();
 9     }
10 
11 }
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对象作为参数

1 package week2;
2 
3 public class Student {
4     String name;
5     int age;
6     int no;
7     int score;
8 
9 }
Student
 1 package week2;
 2 
 3 public class StudentBz2 {
 4 
 5     Student students[] = new Student[30];
 6 
 7     public void addStudent(Student student) {
 8         for (int i = 0; i < students.length; i++) {
 9             if (students[i] == null) {
10                 students[i] = student;
11                 break;
12             }
13 
14         }
15 
16     }
17 
18     public void showStudent() {
19         for (int i = 0; i < students.length; i++) {
20             if (students[i] != null) {
21                 System.out.println(students[i].name + "," + students[i].age
22                         + "," + students[i].no + "," + students[i].score + ",");
23 
24             }
25 
26         }
27 
28     }
29 
30 }
StudentBz2
 1 package week2;
 2 
 3 import java.util.Scanner;
 4 
 5 public class TestStudentBz2 {
 6     public static void main(String[] args) {
 7         StudentBz2 stubz=new StudentBz2();    
 8         Scanner input=new Scanner(System.in);
 9         for (int i = 0; i < 2; i++) {
10             Student stu=new Student();//循环几次出现几个对象
11             System.out.print("请输入姓名:");
12             stu.name=input.next();
13             System.out.print("请输入年龄:");
14             stu.age=input.nextInt();
15             System.out.print("请输入学号:");
16             stu.no=input.nextInt();
17             System.out.print("请输入成绩:");
18             stu.score=input.nextInt();            
19             stubz.addStudent(stu);
20         }
21         System.out.println();
22         
23         stubz.showStudent();
24                 
25     }
26     
27 }
TestStudentBz2

练习实现对客户姓名的排序

 1 package week2;
 2 
 3 import java.util.Arrays;
 4 
 5 public class KH {
 6     
 7     public void sortName(String []names) {
 8         Arrays.sort(names);
 9         
10     }
11 }
KH
 1 package week2;
 2 
 3 public class KHTest {
 4     public static void main(String[] args) {
 5         String []names={"j","sds","sdssf","ssf"};
 6         KH kh=new KH();
 7         System.out.println("排序前");
 8         for (int i = 0; i < names.length; i++) {
 9             if (names[i]!=null) {
10                 System.out.println(names[i]+",");
11                             
12             }
13             
14         }
15         kh.sortName(names);
16         System.out.println("排序后");
17         for (int i = 0; i < names.length; i++) {
18             if (names[i]!=null) {
19                 System.out.println(names[i]+",");
20                 
21                 
22             }
23             
24             
25         }
26     }
27 
28 }
KHTest

带参数练习

 1 package week2;
 2 
 3 public class CustomerBiz {
 4     String []customer=new String[30];
 5     
 6     public void addName(String name) {
 7         for (int i = 0; i < customer.length; i++) {
 8             if (customer[i]==null) {
 9                 customer[i]=name;
10                 break;
11                 
12             }
13         
14             
15         }
16         
17         
18     }
19     
20     
21     public void showNames() {
22         System.out.println("*****************");
23         System.out.println("客户姓名列表");
24         
25         for (int i = 0; i < customer.length; i++) {
26             if (customer[i]!=null) {
27                 System.out.println(customer[i]+",");
28                 
29             }
30             
31             
32         }
33         
34         
35         
36     }
37 
38 }
CustomerBiz
 1 package week2;
 2 
 3 import java.util.Scanner;
 4 
 5 public class TestCustomer {
 6     public static void main(String[] args) {
 7         boolean con = false;
 8         CustomerBiz cust = new CustomerBiz();
 9         Scanner input = new Scanner(System.in);
10 
11         for (int i = 0; i < 3; i++) {
12             System.out.print("请输入客户姓名:");
13             String na = input.next();
14             cust.addName(na);
15             System.out.println("是否继续(y/n)");
16             String n = input.next();
17 
18             if (!n.equals("y")) {
19 
20             }
21 
22         }
23         cust.showNames();
24 
25     }
26 
27 }
TestCustomer

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Servlet案例6:显示用户的上次访问时间

下一篇:SpringMVC源码之Controller查找原理