java中的逻辑控制语句

这里写自定义目录标题


1.
顺序结构

 2.条件分支结构

    (1)如果xxx,就xxx  :if

```java


import
java.util.Scanner;

public
class hy

{

       public static void main(String[] args) 

       {

              //控制台输入数值-JDK提供的类库

              Scanner console = new
Scanner(System.in);

 

              //如果你考满分,就给你买遥控汽车

              int score = console.nextInt();

              if(score == 100){

                     System.out.println("买到遥控汽车了!");

                     System.out.println("买电池!");

              }

       }

}



(2)如果xxx,就xxx,否则xxx  
:if......else

//
课堂案例:

/*

       用户自己输入:商品单价50、和商品数量12

       输入:实付金额600

       如果:应付金额>500,打8折

       输出:找零金额120

 

       升级版: 判断实付金额够不够*/
```java


//控制台输入数值-JDK提供的类库

package com.hy.h2;



import java.util.Scanner;



public class hy2 {

    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);



        int price = console.nextInt();

        System.out.println("请输入商品单价:");

        int count = console.nextInt();

        System.out.println("请输入商品数量:");

        double money = console.nextInt();//实付金额

        System.out.println("请输入实付金额:");

        double sum=price*count; //应付金额

        if(money > 500){

            sum=sum*0.8;

        }

        System.out.println("找零:"+(money-sum));

        if (money>=sum){

            System.out.println("找零:"+(money-sum));

        }else{

            System.out.println("余额不足");

        }

    }

}
 (3)如果xxx,就xxx,否则如果xxx,就xxx  :if-else if-else if......-else
 

```java


import
java.util.Scanner;

public
class hy

{

       public static void main(String[] args) 

       {

              //控制台输入数值-JDK提供的类库

              Scanner console = new
Scanner(System.in);

 

              int month = console.nextInt();

              if(month == 1 || month == 3 ||
month == 5 || month == 7 || month == 8 || month == 10 || month == 12){

                     System.out.println(month+"月,一共31天");

              }else if (month == 4 || month == 6
|| month == 9 || month == 11 ){

                     System.out.println(month+"月,一共30天");

              }else if(month == 2){

                     System.out.println(month+"月,一共28天");

              }else{

                     System.out.println("月份不存在");

              }

       }

}

(5)switch-case 仅用于判断是否相等的情况,只适用于int类型(char\short\byte也可以),JDK1.7之后也支持字符串String"";(一个case后面只能跟一个值)

```java


import
java.util.Scanner;

public
class hy

{

       public static void main(String[] args) 

       {

              //控制台输入数值-JDK提供的类库

              Scanner console = new
Scanner(System.in);

 

              int month = console.nextInt();

              switch(month){

              case 1:

              case 3:

              case 5:

              case 7:

              case 8:

              case 10:

              case 12:

                     System.out.println(month+"月,一共31天");

                 
break;

              case 4:

              case 6:

              case 9:

              case 11:

                     System.out.println(month+"月,一共30天");

                 
break;

              case 2:

                     System.out.println(month+"月,一共28天");

                 
break;

              default:

                     System.out.println("月份不存在");

                 
break;

              }

       }

}



3.循环结构

    (1) for循环

例题:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

```java
public class hy1 {

    public static void main(String[] args) {

        double i=2;

        double j=1;

        double n=0;

        double sum=0;

        double temp;//临时变量

        for(n=1;n<=20;n++){

            sum=sum+i/j;

            temp=i;

            i=i+j;

            j=temp;

        }

        System.out.println("前20项之和为: " + sum);

    }

}


(2)while循环:

特点: 不确定循环次数

例题:有猜数字游戏,其游戏规则为:程序内置一个 1 到 1000 之间的数字作为猜测的结果,由用户猜测此数字。用户每猜测一次,由系统提示猜测结果:大了、小了或者猜对了;直到用户猜对结果,则提示游戏结束。用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0,则游戏终止。

```java


package com.hy.h2;



import java.util.Scanner;



public class hy2 {

    public static void main(String[] args) {

        //猜数字

       // int num=666;//自己给定一个随机数作为猜测结果

        int num = (int) (Math.random() * 1000) + 1;//生成一个随机数作为猜测结果

        System.out.println("请输入数字(1-1000),退出请按0: ");

        Scanner console=new Scanner(System.in);

        int n = console.nextInt();

        //当输入错误时,继续猜数字

        while(n!=num){

            //当输入为0时,结束循环,否则继续判断数字

            if(n==0){

                break;

            }else if(n<num){

                System.out.println("太小了");

            }else if(n>num) {

                System.out.println("太大了");

            }

            System.out.println("请输入数字(1-1000):退出请按0 ");

            n = console.nextInt();

        }

        //提示用户最终结果

        if(n==num){

            System.out.println("恭喜你,猜对了");

        }else{

            System.out.println("很遗憾,请下次继续!");

        }



    }

}


(3)do…while(条件)循环

在一定程度上,两种循环可以互相替换

特殊: 在第一次循环条件就不满足的时候

package
com.hy.h1;



import java.util.Scanner;



public class hy1 {

    public static void main(String[] args) {

        int num=520;

        System.out.println("请输入你猜测的数字(1-1000),退出请按0:");

        Scanner console = new
Scanner(System.in);

        int n=console.nextInt();

        do{

            if(n==0){

                break;

            }

            if(n>num){

                System.out.println("太大了");

            }else if(n<num){

                System.out.println("太小了");

            }

            System.out.println("请输入你猜测的数字(1-1000),退出请按0:");

            n=console.nextInt();

        }while(n!=num);

        if(n==num){

            System.out.println("恭喜你,猜对了!");

        }else{

            System.out.println("很遗憾,请下次继续!");

        }

    }

}

while和do-while语句的区别

·       while循环先判断再执行;

·       do-while循环先执行一次,再判断;

当初始情况不满足循环条件时,while循环一次都不会执行;do-while循环不管任何情况都会至少执行一次。

while和do-while语句的不同仅仅体现在第一次就不满足条件的循环中;如果不是这样的情况,while和do-while可以互换。





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值