JAVA-基础语法

零、基本概念

基本数据类型8种:整数4、浮点数2、字符、布尔值

一、运算

算术运算符 + - * / %

强制转换:(要转换的类型) 原数据

double a = 12.3;
int b = 1 + (int) a; //b=13

自增自减运算:++ --

int a = 10;
int b = a++; //先计算再加1 b=10 a=11

int c = 10;
int d = ++c; //先加1再计算 d=11 c=11

a += 1;
a -= 1;

 二、if switch for while

1. if 条件判断

Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int a = sc.nextInt();
if (a > 0 && a <= 10) {
    System.out.println("a是一个比较小的数:" + a);
} else if (a > 10) {
    System.out.println("a是一个比较大的数:" + a);
} else {
    System.out.println("a不合法");
}

2.switch 条件判断 不写break会导致case穿透

int number = 100;
switch (number){
    case 1:
        System.out.println("number的值为1");
        break;
    case 10:
        System.out.println("number的值为10");
        break;
    default:
        System.out.println("number的值不是1或者10");
        break;
}

3.for 循环

continue:结束本次循环,进入下个循环

break:结束整个循环

for (int i = 1; i <= 10; i++){
    if(i==3) continue;
    if(i==6) break;
    System.out.println("现在打印的是" + i);
}

4.while 循环

while(i <= 100){
    System.out.println(i);
    i++;
}

猜你喜欢

转载自blog.csdn.net/m0_58285786/article/details/129867807
今日推荐