条件语句if,switch,循环for ,while, do while

本文介绍了Java编程中的基本控制结构,包括三位数逆序排列、if-else语句、switch判断、for和while循环的使用,展示了这些结构在实际编程中的应用。
摘要由CSDN通过智能技术生成

首先关于三位数逆序排列

import java.util.Scanner;

public class ThreeEyes {
    public static void main(String[] args) {
        System.out.println("输入一个三位数");
        int a = 0;
        Scanner scanner = new Scanner(System.in);
        int s = scanner.nextInt();
        while(s!=0){
           a *=10;
           a += s%10;
           s /=10;

        }
        System.out.println(a);

    }
}

利用中间变量判断两个数大小

import java.util.Scanner;

public class IfDemo0125 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入两个数");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        /*if (a>=b)
        {
            System.out.println(a);
        }
        if (b>a){
            System.out.println(b);
        }*/
        /*int m = a > b ? a : b;
        System.out.println(m);
*/
        int max = a;
        if(a < b)
        {
            max = b;
        }
        System.out.println(max);
    }
}

if else语句的应用

import java.util.Scanner;

public class IfElseElseDemo001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的高考考试成绩");
        int score = scanner.nextInt();
        if (score >= 650) {
            System.out.println("能上清北");
        } else if (score >= 550 && score < 650) {
            System.out.println("");
        } else if (score >= 450 && score < 550) {
            System.out.println("");
        } else if (score >= 350 && score < 450) {
            System.out.println("");
        } else
            System.out.println("");
    }

}

switch语句判断某一月的天数

import java.util.Scanner;

public class SwitchCaseDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入月份");
        int mouth = scanner.nextInt();
        switch(mouth){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.println("31天");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println("30天");
                break;
            default :
                System.out.println("28天");
                break;

        }
    }
}
for while dowhile 应用
public class ForDemo01 {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++){
            if(i == 5){
                continue;
            }
            System.out.print(i + " ");

        }
        for (int i = 1; i <=100; i++){
            if (i % 2 == 0)
            {
                System.out.print(i + " ");
            }
        }

    }
}
import java.util.Scanner;

public class WhileDemo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = 1;
        while(true)
        {
            System.out.println("nizhengzai" );
            String str = scanner.next();
            if("fou".equals(str)){
                 break;
            }
            if("shi".equals(str)){
                System.out.println("nizaipaodi" + i +"quan");
            }
        }
    }
}
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println(i + " ");
            i++;
        }
        while (i<=0);
    }
}
public class DoubleForDemo01 {
    public static void main(String[] args) {
        for(int i = 1;i <= 9;i++){
            for(int j = 1; j <= i; j++){
                System.out.print("i * j = " + i*j + " ");
            }
            System.out.println();
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值