FOR循环打印三角形
package xhjava;
public class TestDemo01 {
public static void main(String[] args) {
// 打印三角形
for (int i = 0; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("&");
}
for (int j = 1; j < i; j++){
System.out.print("&");
}
System.out.println();
}
}
}
// ==========================
&
&&
&&&&
&&&&&&
&&&&&&&&
&&&&&&&&&&
定义方法
public class DefDome01 {
public static void main(String[] args) {
int sum = add(1, 2);
System.out.println(sum);
}
// 加法 static 类变量
public static int add(int a, int b) {
return a + b;
}
}
// ==================
3
方法调用
package xhjava;
public class DefTest02 {
public static void main(String[] args) {
int size = max(1, 1);
System.out.println(size);
}
public static int max(int number, int number2) {
int result = 0;
if (number == number2) {
System.out.println("num=num2");
return 0;
}
if (number > number2) {
result = number;
} else {
result = number2;
}
return result;
}
}
方法重载(方法名称相同,根据数据类型不同,来判断使用那个方法)
package xhjava;
public class DefTest03 {
public static void main(String[] args) {
int max = max(12, 1);
System.out.println(max);
}
public static int max(int number, int number2) {
int result = 0;
if (number == number2) {
System.out.println("num=num2");
return 0;
}
if (number > number2) {
result = number;
} else {
result = number2;
}
return result;
}
public static double max(double number, double number2) {
double result = 0;
if (number == number2) {
System.out.println("num=num2");
return 0;
}
if (number > number2) {
result = number;
} else {
result = number2;
}
return result;
}
}