package com.math;
import java.util.Scanner;
/*
* 方法其實就是完成特定功能的代碼塊
* 定義格式
* 修飾符 返回值類型 方法名(參數類型 參數名1,參數類型,參數值2,){
* 方法體;
* return 返回值;
* }
*/
public class MethodDemo {
public static void main(String[] args) {
// TODO 簡單的調用方法
int i=10;
int j=20;
//三種調用方法-1-單獨調用,2-輸出調用,3-賦值調用
System.out.println(sum(i,j));
//返回最大值
int max=getMax(3,5);
System.out.println(max);
//判斷函數是否相等
Scanner sc=new Scanner(System.in);
System.out.println("請輸入第一個數字:");
int x=sc.nextInt();
System.out.println("請輸入第二個數字:");
int y=sc.nextInt();
boolean result=compare(x,y);
System.out.println(result);
//三個數最大值
System.out.println("請輸入第一個數字:");
int m=sc.nextInt();
System.out.println("請輸入第二個數字:");
int n=sc.nextInt();
System.out.println("請輸入第三個數字:");
int q=sc.nextInt();
int max1=compareMax(m,n,q);
System.out.println(max1);
}
//求和方法
public static int sum(int a,int b) {
int c=a+b;
return c;
}
//返回最大值方法
public static int getMax(int a,int b) {
if(a>b) {
return a;
}else {
return b;
}
}
//判斷兩個數是否相等
public static boolean compare(int a,int b) {
if (a==b) {
return true;
}else {
return false;
}
}
//三個數中的最大值
public static int compareMax(int a,int b,int c) {
if (a>b) {
if(a>c) {
return a;
}else {
return c;
}
}else {
if (b>c) {
return b;
}else {
return c;
}
}
}
}
訓練2-void修飾方法的調用
package com.math;
import java.util.Scanner;
/*
* void修飾方法的調用-只能單獨調用
*/
public class MethodTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
printWord();
}
public static void printWord(){
System.out.println("請輸入打印的次數:");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
System.out.println("請輸入要打印的語句:");
String j=sc.next();
for (int x=0;x<i;x++) {
System.out.println(j);
}
}
}
訓練3-打印水仙花數
package com.math;
/*
* 打印水仙花數
*/
public class MethodTest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
printFlower();
}
public static void printFlower() {
for (int x=100;x<1000;x++) {
int ge=x%10;
int shi=x/10%10;
int bai=x/10/10%10;
if ((ge*ge*ge+shi*shi*shi+bai*bai*bai)==x) {
System.out.println(x);
}
}
}
}
訓練4-方法的重載
package com.math;
/*
* 方法的重載-在同一個類中允許方法名相同的情況,只是參數類型或者參數個數不同,與返回值無關
*/
public class MethodTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=10;
int j=20;
int m=30;
//三個數的和,重載使用
int result = sum(i,j,m);
}
//求和方法
public static int sum(int a,int b) {
int c=a+b;
return c;
}
public static int sum(int a,int b,int c) {
int he=a+b+c;
return he;
}
}
// 方法的參數如果是基本數據類型,形式參數的改變不影響實際參數。
//形式參數-用於接收實際參數的變量
//實際參數-實際參與運算的變量
訓練5-傳參問題(形式參數和實際參數)
package com.math;
/*
* 方法的參數如果是基本數據類型,形式參數的改變不影響實際參數。
形式參數-用於接收實際參數的變量
實際參數-實際參與運算的變量
* 如果參數是引用數據類型,形式參數的改變直接影響實際參數
*/
public class MethodTest3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {1,2,3,4,5};
for (int x=0;x<arr.length;x++) {
System.out.println(arr[x]);
}
change(arr);
for (int x=0;x<arr.length;x++) {
System.out.println(arr[x]);
}
}
public static void change(int[] arr) {
for(int x=0;x<arr.length;x++) {
if(arr[x]%2==0) {
arr[x]*=2;
}
}
}
}
訓練5-編寫簡單的函數對數組進行操作
package com.math;
/*
* 函數對數組進行操作
*/
public class MethodTest4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {1,2,3,4,5};
browe(arr);
//最值
int max = getMax(arr);
System.out.println(max);
//求和
int sum = sum(arr);
System.out.println(sum);
}
//遍歷數組方法
public static void browe(int[] arr) {
for (int x=0;x<arr.length;x++) {
System.out.println(arr[x]);
}
}
//方法求數組的最值
public static int getMax(int[] arr) {
int max=arr[0];
for (int x=1;x<arr.length;x++) {
if (arr[x]>max) {
max=arr[x];
}
}
return max;
}
//方法對數組求和
public static int sum(int[] arr) {
int sum=0;
for (int x=0;x<arr.length;x++) {
sum+=arr[x];
}
return sum;
}
}