【JavaSE】_2.程序逻辑控制

目录

1.顺序结构

2.分支结构

2.1 if else语句

2.2 switch 语句

3.循环结构

3.1 while 循环

 3.2 for 循环

3.2 continue 与 break

3.3 do...while 循环

4.输入输出

4.1 输出到控制台

4.2 从键盘上输入

4.2.1  读入一个字符:

4.2.2  用Scanner读取字符串、整数、浮点数:

4.2.3  使用Scanner循环读取N个数字:

 4.3 简单应用之猜数字游戏


1.顺序结构

顺序结构即:程序的执行和代码的书写顺序有关;

与C语言顺序结构基本相同,不再进行详细解释与示例;

        System.out.print("Hello");
        System.out.print(" ");
        System.out.print("world");
        System.out.print("!");

输出结果为:

2.分支结构

2.1 if else语句

分支结构又称选择结构,其基本格式与C语言基本相同,此处不再赘述,作以下示例:

代码示例1:判断一个数是奇数还是偶数:

        Scanner scanner=new Scanner(System.in);
        int num=scanner.nextInt();
        if(num%2==0){
            System.out.println(num+" is an even");
            }
        else{
            System.out.println(num+" is an odd");
        }

输出结果为:

代码示例2:判断一个年份是闰年还是平年

        public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year = scanner.nextInt();
        if(((year%4==0)&&(year%100!=0))||(year%400==0)){
            System.out.println(year+" is a leap year");
        }
        else{
            System.out.println(year+" is a common year");
        }

 输出结果为:

注:悬垂else问题:

        int x=10;
        int y=20;
        if(x==10)
            if(y==10)
                System.out.println("aaa");
        else
                System.out.println("bbb");

输出结果为:

  

else匹配时只会与距离最近且尚未匹配的if语句进行匹配对应,与代码书格式直观显示的对齐位置没有关系;

2.2 switch 语句

        Scanner scanner = new Scanner(System.in);
        int num= scanner.nextInt();
        switch(num){
            case 1:
                System.out.println("aaa");
                break;
            case 2:
                System.out.println("bbb");
                break;
            default:
                System.out.println("error");
                break;

Java的switch-case语句与C语言的基本相同,但也有不同:

switch语句的参数不能是一个复杂参数, 不能作为switch语句参数的数据类型有:long float double boolean ,诸如byte  short  int  char  String  枚举(JDK1.5后引入)等等都可以作为switch语句的参数;

3.循环结构

Java循环结构语句与C语言也基本相同,此处仅做简单示例:

3.1 while 循环

代码示例1:求整数1~10的和:

        int i=1;
        int sum=0;
        while(i<=10){
            sum=sum+i;
            i++;
        }
        System.out.println("sum="+sum);

输出结果为:

 代码示例2:求5的阶乘:

        int n=5;
        int i=1;
        int tmp=1;
        while(i<=n){
           tmp=tmp*i;
           i++;
        }
        System.out.println(tmp);

输出结果为:

代码示例3:求1!+2!+3!+4!+5!:

        int n=5,i=1,tmp=1,sum=0;
        while(i<=n){
            tmp*=i;
            sum+=tmp;
            i++;
        }
        System.out.println(sum);

 输出结果为:

代码示例4:打印1~100之间所有3的倍数:

        int i=1;
        while(i<=10){
            if(i%3==0){
                System.out.print(i+"\t");
            }
            i++;

 输出结果为:

 3.2 for 循环

代码示例1:求整数1~10的和:

        int i=0,sum=0;
        for(i=1;i<=10;i++){
            sum+=i;
        }
        System.out.println("sum="+sum);

输出结果为:

代码实例2:求5的阶乘:

        int i=0,n=5,ret=1;
        for(i=1;i<=5;i++){
            ret*=i;
        }
        System.out.println(ret);

 输出结果为:

代码示例3:求1!+2!+3!+4!+5!:

        int i=0,n=5,ret=1,sum=0;
        for(i=1;i<=n;i++){
            ret*=i;
            sum+=ret;
        }
        System.out.println("sum="+sum);

 输出结果为:

代码示例4:打印1~100之间所有3的倍数:

        int i=0;
        for(i=1;i<=10;i++){
            if(i%3==0){
                System.out.print(i+"\t");
            }
        }

输出结果为:

3.2 continue 与 break

Java的continue与break用法与C语言也相同,continue跳出本次循环,break跳出整个循环;

此处不再详细解释,作一示例:

请编写代码输出1~100之间既能被3整除也可以被5整除的数:

方法1:

        int i=0;
        for(i=1;i<=100;i++){
            if((i%3!=0)||(i%5!=0)){
                continue;
            }
            else{
                System.out.print(i+"\t");
            }
            System.out.print("\n");  
        }

 方法2:

        int i=0;
        for(i=1;i<=100;i++){
            if((i%3==0)&&(i%5==0)){
                System.out.print(i+"\t");
            }
            System.out.print("\n");     
        }

以上两种方法输出结果均为:

3.3 do...while 循环

do...while循环在实际中应用很少,此处仅作一简单示例:

        int i=0,sum=0;
        do{
            sum+=i;
            i++;
        }while(i<=10);
        System.out.println("sum="+sum);

 do...while循环与while和for循环的不同是,do...while循环至少被执行一次;

4.输入输出

4.1 输出到控制台

//方法1:
System.out.println("hello world");
//输出末尾自带一个换行符\n

//方法2:
System.out.print("hello world");
//输出末尾不带换行符

//方法3:
System.out.printf("hello world");
//格式化输出

4.2 从键盘上输入

4.2.1  读入一个字符:

public static void main(String[] args) throws IOException {
        System.out.print("Enter a char:");
        char i=(char)System.in.read();  //alt+enter
        System.out.println("The char is "+i);
    }

4.2.2  用Scanner读取字符串、整数、浮点数:

        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println("The number you input is "+a);
        double b=scanner.nextDouble();
        System.out.println("The number you input is "+b);

试运行输出结果为:

 注:(1)字符串优先读取问题:

尽量优先读取字符串再读取其他数据类型,否则先于字符串读取的其他数据类型输入完成后从键盘输入的enter回车键会作为参数传递给 String str=scanner.nextLine();

(2)String str=scanner.next() 读取语句:

试运行以下代码:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str=scanner.next();
        System.out.println("The string you put in is "+str);
        int a = scanner.nextInt();
        System.out.println("The number you put in is "+a);
    }

输入 hello world,报错如下: 

 (3)Java将Scanner视为一个文件操作,在使用完Scanner后建议采用scanner.close();进行关闭,实际使用中此操作可有可无;

4.2.3  使用Scanner循环读取N个数字:

        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()){
            int n= scanner.nextInt();
            System.out.println(n);
        }

输出结果为:

(在IDEA环境下,Ctrl+D键即可结束循环)

 4.3 简单应用之猜数字游戏

public static void main(String[] args) {
        //guess the digit
        Random random = new Random(); //产生一个随机数
        int rand = random.nextInt(100);//[0~100)
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入你猜的数字:");
            int n=scanner.nextInt();
            if(n<rand){
                System.out.println("猜小了");
            }
            else if(rand==n){
                System.out.println("猜对了");
                break;
            }
            else{
                System.out.println("猜大了");
            }
        }
    }

注:随机数与随机种子:

(1)当random参数为空时,重新运行程序会随机产生一个随机数;

(2)当给random传递一个数字作为参数时,就会产生伪随机的随机种子:

这个产生的数字是根据random参数产生的随机数,当重新运行程序时,该数字不会变化;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值