JAVA_2
一、JAVA控制语句
1.if-else
2.switch
3.while
4.for
public class test3_processControl {
public static void main(String[] args) {
// System.out.println(Math.random());//0-1随机数
int num = (int) (Math.random() * 26);
char zimu = 'a';
zimu = (char) (zimu + num);
if (zimu == 'a' || zimu == 'e' || zimu == 'i' || zimu == 'o' || zimu == 'u') {
System.out.println("元音" + zimu);
} else {
System.out.println("辅音" + zimu);
}
//switch可以接收整数枚举字符串
switch (zimu) {//巧用switch,case不写break会一直向下执行
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("元音" + zimu);
break;
default:
System.out.println("辅音" + zimu);
}
while (num < 10) {
num++;
}
char c = 'a';
for (int i = 0; i < 26; i++) {
char tmp = (char) (c + i);
System.out.print(tmp + "'");
}
for (int n = 1; n <= 9; n++) {
for (int m = 1; m <= n; m++) {
System.out.print(m + "*" + n + "=" + (m * n) + "\t");
}
System.out.print("\n");
}
}
}
二、JAVA方法定义和重载
1.重载overload是指类定义多个同名方法调用时根据不同参数自动匹配
2.形参类型/形参个数/形参顺序不同才能构成重载
public class test4_method {
public static void main(String[] args) {
int sum = add(100, 200);
System.out.println(sum);
print();
}
public static int add(int n1, int n2) {
return n1 + n2;
}
public static void print() {
System.out.println("hellow");
}
public static double add(double n1, int n2) {
return n1 + n2;
}
public static double add(double n1, int n2, int n3) {
return n1 + n2 + n3;
}
public static double add(int n1, double n2) {
return n1 + n2;
}
}