Java方法
方法的定义和调用 先构建方法 方法命名规则
package com.whs.method;
public class Demo39 {
public static void main(String[] args) {
test();
}
public static void test() {
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i+"\t");
}
if (i % (5 * 3) == 0) {
System.out.println();
}
}
}
}
java方法重载(方法名相同(可以重名)参数不相同(int || double 等等不同))
package com.whs.method;
public class Demo41Args {
public static void main(String[] args) {
double max = test(20.9, 80.1);
System.out.println(max);
}
public static int test(int number1, int number2) {
int result = 0;
if (number1 == number2) {
System.out.println("number1==number2");
return 0;
}
if (number1 > number2) {
result = number1;
}else {
result = number2;
}
return result;
}
public static double test(double number1, double number2) {
double result = 0;
if (number1 == number2) {
System.out.println("number1==number2");
return 0;
}
if (number1 > number2) {
result = number1;
}else {
result = number2;
}
return result;
}
}
|