JAVA流程控制语句

导包(输入)import  java.util.Scanner;

String 自定义=input.next();

System.out.println("Your nanme is:"+自定义);

概述: 顺序结构  选择结构{if-else(双分支)    switch}   循环结构   跳转   多重循环  方法:定义、调用、重载。 递归算法

if语句(单分支选择结构)

if语句进行执行若为true则执行下面的语句否则跳过该语句

if语句

import java util.Scanner;
public class kongshan{
    public static void main(String[]args){

        double i = 6 * Math.random();

        double j = 6 * Math.random();

        double k = 6 * Math.random();

        int count = (int) (i + j + k);
     }    

        if(count > 15 ) {

        System.out.println(“今天手气不错”);

        if(count >= 10 && count <= 15) {         //错误写法:10<count<15

        System.out.println("今天天气很一般");

    }
}

import java.util.Scanner;
    public class kongshan{
        public static void main (String[]args){
            if(count < 10) {
            System.out.println(“今天手气不怎么样”);
            }
            System.out.println("得了" + count + "分");
    }
}

/*必须是布尔表达式条件结构必须是布尔值建议加上花括号,如不加则只对第一句话有效!

if-else(双分支选择结构)

当条件表达式为真时,执行语句块1,否则,执行语句块2.也就是else部分

       条件:if("") {

      语块1:System.out.println("");

}else{

        语块2:System.out.println(""):

     }
*/

例1:
import java.util.Scanner;
    public class work{
    public static void mian(String[] args ){
            //string age1=(string )(Math.random())
            String age= sc.nextstring();
            Scanner sc =new Scanner(System.in);
            System.out.prstringln("年龄大于7岁可以搬桌子。");
            //System age=sc.nextLine();
            if (age>7){
                System.out.prstringln("可以搬桌子");
            }else if(age>5){ 
                System.out.prstringln("输入性别:(boy/girl)");
                String sex=sc.nextstring();
            if(sex.equals("boy")){
                System.out.prstringln("年龄大于5岁,是boy可以搬桌子。");
            }else{
                System.out.prstringln("不行你太小了");
            }
        }
    }

    System.out.println("男生大于5岁也可以搬桌子。")
            int gender=sc.nextInt();
            String sex = (int)(Math.random()*2) == 0 ? "girl" : "boy";
            
            if(sex == "girl"){
               System.out.println("欢迎进入女子组");
            }else{
               System.out.println("欢迎进入男子组");
            }
        }else{
               System.out.println("对不起,您的成绩太菜了,被淘汰了!");
        }
        
            
}

   import java.util.Scanner;    
       public class kong{
           public static void main(String[] args){
                int num1 = (int)(Math.random()*10);
                Scanner sc = new Scanner(System.in);
                //System.out.println("百位数是:" + ((num / 100) % 10));
                System.out.println("欢迎光临腾讯互联抽奖系统");
                System.out.println("请输入四位数");
                int num = sc.nextInt();
                if(((num / 100) % 10)==num1){    
                System.out.println("恭喜您中奖了!");            
        } else{
                System.out.println("很遗憾没有中奖");
            }
        }
    } 

public class work2{
     public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        System.out.println("输入你的金额:");
        //int money = in.nextInt();
        int money = sc.nextInt();
        if(money >=500){
            System.out.println(money +"万"+"我买凯迪拉克");
        }else if(money>=100){
            System.out.println(money +"万"+"我买帕萨特");
        }else if(money>=50){
            System.out.println(money +"万"+"我买依兰特");
        }else if(money>=10){
            System.out.println(money +"万"+"我买奥托");
        } else{
            System.out.println("我买老年代步车");
        }
    }
}

public class work2{
   public static void main(String [] args){
      Scanner sc=new Scanner(System.in);
      System.out.println("按1:拨爸爸的号,按2:拨妈妈的号,按3:拨爷爷的号,按4:拨奶奶的号");
        String phone=sc.nextInt();
        if(phone=1){
            System.out.println("拨爸爸的号");
        }else if (phone=2){
            System.out.println("拨妈妈的号");
        }else if (phone=3){
            System.out.println("拨爷爷的号");
        }else if (phone=4){
            System.out.println("拨奶奶的号");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值