JAVA基础学习20171030-流程控制

1.流程控制语句:顺序、选择、循环

2.选择结构
If(条件){
语句1;
语句2;
…}

If(条件){
语句1;
语句2;
…}
else{
语句1;
语句2;
…}

代码:

package com.imooc.flow;

import java.util.Scanner;

public class ScoreAssess {
    public static void main(String[] args) {
        //成绩大于等于90输出优,大于等于80小于90输出良,大于等于60小于80输出中,小于60输出不及格
        System.out.println("Please input score");
        Scanner sc=new Scanner(System.in);
        int score=sc.nextInt();
        if(score>=90)
            {System.out.println("It is excellent");}
        if(score<90&score>=80)
            {System.out.println("It is good");}
        if(score<80&score>=60)
            {System.out.println("It is mediun");}
        if(score<60)
            {System.out.println("It is fail");}

        if(score>=90)
            {System.out.println("It is excellent");}
        else if(score>=80)//amount to score>=80&score<90
            {System.out.println("It is good");}
        else if(score>=60)
            {System.out.println("It is mediun");}
        else
            {System.out.println("It is fail");}

    }
}

3.嵌套if结构

代码:

package com.imooc.flow;

import java.util.Scanner;

public class IntCompare {
    public static void main(String[] args){
        System.out.println("Please input an integer a");
        Scanner cs=new Scanner(System.in);
        int a=cs.nextInt();
        System.out.println("Please input an integer b");
        Scanner sc=new Scanner(System.in);
        int b=sc.nextInt();
        if(a>b){
            System.out.println("int:"+a+">"+"int:"+b);
        };
        if(a==b){
            System.out.println("int:"+a+"="+"int:"+b);
        };
        if(a<b){
            System.out.println("int:"+a+"<"+"int:"+b);
        };


        if(a!=b){
            if(a>b){
                System.out.println(a+">"+b);
            }else{
                System.out.println(a+"<"+b);
            }
        }else{
            System.out.println(a+"="+b);
        }

    }

}

4.if和switch的区别
If结构:
判断条件是布尔类型
判断条件是一个范围

switch结构:
判断条件是常量值

switch结构:
switch(表达式){
case 常量表达式1:
语句1;break;
case 常量表达式2:
语句2;break;
default:
语句3;
}

代码:

package com.imooc.flow;

import java.util.Scanner;

public class WeekDemo1 {
    public static void main(String[] args) {
        System.out.println("Please input an integer between 1 and 7:");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        switch (a) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        case 6:
            System.out.println("Saturday");
            break;
        case 7:
            System.out.println("Sunday");
            break;
        default:
            System.out.println("The integer: " + a + " is not between 1 and 7!");
        }
    }

}

代码:

package com.imooc.flow;

import java.util.Scanner;

public class WeekDemo2 {
    public static void main(String[] args) {
        System.out.println("Please input week");
        Scanner sc = new Scanner(System.in);
        String week = sc.next();
        week=week.toUpperCase();//改为大写
        switch (week) {
        case "MONDAY":
            System.out.println("星期一");
            break;
        case "TUESDAY":
            System.out.println("星期二");
            break;
        case "WEDNESDAY":
            System.out.println("星期三");
            break;
        case "THURSDAY":
            System.out.println("星期四");
            break;
        case "FRIDAY":
            System.out.println("星期五");
            break;
        case "SATURDAY":
            System.out.println("星期六");
            break;
        case "SUNDAY":
            System.out.println("星期日");
            break;
        default:
            System.out.println("The word: " + week + " is wrong!");
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值