选择结构
Scanner 获取键盘输入
| 属性 | 说明 | 
|---|---|
| next | 字符串 | 
| nextInt | 整数 | 
| nextLine | 可接受空格 字符串 | 
public class Demo4 {
    public static void main(String[] args) {
        //实例化对象
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入账号:");
        String number = scan.next();
        System.out.print("请输入密码:");
        int passwd = scan.nextInt();
        System.out.println("账号:" + number + "密码:" + passwd);
        //关闭流
        scan.close();
    }
}Random随机数
public class Demo5 {
    public static void main(String[] args) {
        //随机数 0-9
        int random = (int)(Math.random()* 10);
        System.out.println("生成0-9的随机数为:" +random);
    }
}if...else语句
if...else 语法格式如下:
if(布尔表达式){
    //如果表达式为true 则执行这行代码
} else {
    //如果表达式为false 则执行这行代码
}public class Demo5 {
    public static void main(String[] args) {
        //从键盘获取用户输入
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入账号:");
        String number = scan.next();
        System.out.print("请输入密码:");
        int pwd = scan.nextInt();
        //进行判断 获取用户输入 如果输入的账号密码与条件成立 那就登录成功 否则就登录失败
        if ("admin".equals(number) && pwd == 123456) {
            System.out.println("登录成功");
        }else {
            System.out.println("用户名或密码不正确,请重新输入");
        }
    }
}if-else-if 语句
if-else-if 语法格式如下:
if (条件1) {
    //如果条件1为true 则执行这块代码
}elif if (条件2) {
    //如果条件2为true 则执行这块代码
}elif if (条件3) {
    //如果条件3为true 则执行这块代码
}else {
    //如果都不满足 则执行这块代码
}public class Demo6 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入考试分数:");
        int score = scan.nextInt();
        if (score > 100 || score < 0) {
            System.out.println("请输入正确的分数");
        } else if (score >= 90) {
            System.out.println("A");
        } else if (score >=70) {
            System.out.println("B");
        } else if (score >= 60) {
            System.out.println("C");
        }else {
            System.out.println("D");
        }
        scan.close();
    }
}public class Demo6 {
    public static void main(String[] args) {
        int rdm = (int) (Math.random() * 10);
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入要猜的数字:");
        int random = scan.nextInt();
        //如果 输入的随机数大于等于10或者小于0 那就不符合规范
        //如果 随机大于输入的数字那就是猜小了 如果是随机数小于输入的数字 那就是猜大了 如果一样那就是猜对了
        if (random >=10 || random <0) {
            System.out.println("请输入10以内的数字");
        } else if (rdm > random) {
            System.out.println("小了");
        } else if (rdm < random) {
            System.out.println("大了");
        }else if (rdm == random) {
            System.out.println("恭喜你 猜对了");
        }
        scan.close();
    }
}if 嵌套语句
if 嵌套语句如下:
if (外则条件) {
    if (内则条件) {
    //外则条件为true 执行内则条件 内则条件为true 执行这块代码
    }else if (条件1) {
    //条件1为true 执行这块代码
    }
}else { 
    //外则条件不满足 则执行这块代码
}public class Demo6 {
    public static void main(String[] args) {
        //男女不得早于22周岁
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入年龄:");
        int age = scan.nextInt();
        System.out.print("请输入性别:");
        String sex = scan.next();
        //如果输入的年龄大于22岁才可以正常结婚 不然无法结婚 如果年龄大于22 性别男或女都可以结婚    
        if (age >= 22){
            if ("男".equals(sex)) {
                System.out.println("男生可以结婚了");
            } else if ("女".equals(sex)) {
                System.out.println("女生可以结婚了");
            }else {
                System.out.println("请输入正确性别");
            }
        }else {
            System.out.println("无法结婚");
        }
    }
}swich语句
swich语句如下:
    switch(表达式) { //表达式 int、 short、byte、char、枚举、 String类型
                      case 目标值1:
                             执行语句1
                      break;
                      case 目标值2:
                            执行语句2
                      break;
                      case 目标值n:
                           执行语句n
                      break;
                     default:
                          执行语句n+1  
                      break;public class Demo6 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入第一个整数:");
        int num = scan.nextInt();
        System.out.print("请输入第二个整数:");
        int num1 = scan.nextInt();
        System.out.print("请输入运算符 (+ - / * %) :");
        String str = scan.next();
        int result = 0;
        switch (str) {
            case "+":
                result = num + num1;
                break;
            case "-":
                result = num - num1;
                break;
            case "*":
                result = num * num1;
                break;
            case "/":
                result = num / num1;
                break;
            case "%":
                result = num % num1;
                break;
            default:
                System.out.println("请输入正确的运算符");
        }
        System.out.println("运算结果是:" + num + str + num1 + "=" + result);
    }
}循环结构
while循环
while语句如下:
//先判断条件后执行
while (判断条件) {
    //如果为True 则执行这块代码
}public class Demo6 {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println("likedx.com");
            i++;
        }
    }
}public class Demo6 {
    public static void main(String[] args) {
        //计算1-100的和
        int one = 1;
        int sum = 0;
        while (one <= 100) {
            sum +=one;
            one++;
        }
        System.out.println("1-100的和为:" + sum);
    }
}do..while循环
do...while循环语句如下:
//先执行后进行判断
do {
    //执行语句
} while(判断条件);public class Demo6 {
    public static void main(String[] args) {
        //计算1-100的和
        int one = 1;
        int sum = 0;
        do {
            sum = sum + one;
            one++;
        }while (one <= 100);
        System.out.println("1-100的和为:" + sum);
    }
}for循环
for循环语句如下:
for(初始化表达式;布尔表达式;更新表达式) {
    //语句块  
    //运行结果:初始化表达式---->布尔表达式---->语句块---->更新表达式---->布尔表达式---->语句块 当为false 结束运行
}public class Demo7 {
    public static void main(String[] args) {
        //输出10以内的奇数
        for (int i = 1; i < 10; i+=2) {
            System.out.println(i);
        }
        System.out.println("==========");
        
        //输出10以内的偶数
        for (int i = 2; i <= 10; i+=2) {
            System.out.println(i);
        }
    }
}public class Demo7 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int score; //获取用户键盘输入
        int avg; //获取平均分
        int sum = 0; //获取和
        for (int i = 0; i < 5; i++) {
            System.out.print("请输入5门功课第" + (i+1) + "门课的成绩:");
            score = scan.nextInt();
            sum += score;
        }
        avg = sum / 5;
        System.out.println("和为:" + sum + " 平均分为:" + avg);
    }
}for双层循环
for双层循环语句如下:
for (int i = 0; i < 1; i++) {
    for (int j = 0; j < 2 ; j++) {
        System.out.println("一轮循环");
    }
    //for i 外层 for j 内层
    //当外层循环循环1次时 内层循环会循环1轮
}双层for循环的特点:内层循环一遍,相当于外层循环只执行一次。
public class Demo7 {
    public static void main(String[] args) {
        //打印99乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i*j + "\t");
            }
            System.out.println();
        }
    }
}break和continue
break:终止循环
continue:跳出本次循环 继续下一次循环
public class Demo7 {
    public static void main(String[] args) {
        //打印10以内的奇数 如果到7就跳出循环
        for (int i = 1; i <= 10; i+=2) {
            if (i == 7) {
                System.out.println("我已经到" + i + "了 跳出循环");
                break;
            }
            System.out.println(i);
        }
        System.out.println("==================");
        //打印10以内的奇数 如果到7就跳出本次循环 继续循环下一次
        for (int i = 1; i <= 10; i+=2) {
            if (i == 7) {
                System.out.println("我已经到" + i + "了 继续下一次循环");
                continue;
            }
            System.out.println(i);
        }
    }
}
One comment
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com