2.1語法 public 返回值類型 方法名(){ //方法體 } 2.2方法的調用語法 對象名.方法名 計算平均分和總成績 1 public class Score { 2 /** 3 * 創建類 ScoreCalc 編寫方法實現各功能 編寫測試類 4 * 從鍵盤接收三門課分數,(java c# ...
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