Day-2:Java基础—分支结构

1.顺序结构

        选择结构(分支结构):

                if: if(关系表达式){语句体}:表达式判定为true时,输出语句体

                if-else

                if-else if ---else

2.选择结构:

                switch 经常与case、break结合使用;

     

        无论是if、if else、if-else if-else还是switch,我们首先应该清楚问题的判定条件,去选择合适的语句结构,病牢记初学时易错的点,反复记忆和练习,在后期代码编写中更是要做到代码精简。  

1.if--else语句训练:

         

                “密码账户登陆提示”

                        条件介绍:

      import java.util.Scanner; 

        Scanner sc = new Scanner(System.in);

		System.out.print("请输入第一个数:");
		int a = sc.nextInt();

		System.out.print("请输入第二个数:");
		int b = sc.nextInt();

		if(a == b) {
			System.out.println("你摸到一个对子");
			
		}
		else {
			System.out.println("你的牌不是对子");
		}

                “逢七过”小游戏:

                        推荐介绍:用户输入相应范围内的数值,数字包含7或7的倍数时,输出“”,使用if--else语句判断;

package com.gy;

import java.util.Scanner;

public class IfDemo2 {


    public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);

        System.out.print("请输入1000以内的整数:");
		
		int h = sc.nextInt();
		
		if(h%7==0 || h%10/7==0 || h%100/7==0 || h%1000/7==0) {
			
			System.out.println("过");
			
		}else {
			System.out.println(h);
		}
		
	}
}

                “活动充值!”

                        条件介绍:简易实现,固定金额充值送费活动,并计算和输出充值后的余额;

package com.gy;
import java.util.Scanner;
public class chongzhi {

	public static void main(String[] args) {
		//余额26;充50送20 充100送50 充500送300
		Scanner sc = new Scanner(System.in);
		int money = 26;
		System.out.println("请输入充值金额:");
		int a = sc.nextInt();
		
		//满足充值活动金额
		int b = 20;
		int c = 50;
		int d = 300;
		
		if(a==50) {
			System.out.println("充值成功 您目前余额为:"+(a+b+money));
		}else if(a==100 ) {
			System.out.println("充值成功 您目前余额为"+(a+c+money));
		}else if (a==500) {
			System.out.println("充值成功 您目前余额为:"+(a+d+money));
		}else {
			System.out.println("充值成功 您目前余额为:"+(a+money));
		}
		
	}

}

2.switch--case语句:

      案例:用switch判断月份;

               注意:break;的使用,更不要胡忽略!编译进入到switch后没有break时会继续执行并输出!所以我们要谨记不要缺少break;

        //1 3 5 7 8 10 12:31天
        //2:28天
        //4 6 9 11:30天

switch语句判断月份天数的代码实现如下:

package com.gy;
import java.util.Scanner;
public class Yuefen {

	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入月份:");
		int m = sc.nextInt();
		
		switch(m) {
		
		case 1:       
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			System.out.println("本月天数为:"+31+"天");
			break;    //跳出switch语句
		case 4:
		case 6:
		case 9:
		case 11:
			System.out.println("本月天数为:"+30+"天");
			break;
		case 2:
			System.out.println("本月天数为:"+28+"天");
		default:
			System.out.println("月份输入错误");
			break;
		}
		
		
		
		System.out.println("请输入你想查询的月份:");
		int a = sc.nextInt();
		
		switch(a) {
		case 3:
		case 4:
		case 5:
			System.out.println("该月份为:春季");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("该月份为:夏季");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("该月份为:秋季");
			break;	
		case 12:
		case 1:
		case 2:
			System.out.println("该月份为:冬季");
			break;
		default:
			System.out.println("输入错误");
			break;
		}
		
	}

}

3.if--else if--else语句:

                如果满足条件1,程序输出内容1;

                如果满足条件2,程序输出内容2;

                如果条件1和条件2都不满足,程序输出内容3;

结构展示:
if(判断条件1){
    输出内容1;
    }else if(判断条件2){
        输出内容2;
    }else{
        输出内容3;
}

案例:       

        “会员积分判定会员等级”

                条件介绍:会员:积分<=500;

                                  白银会员:500<积分<=2000;

                                  黄金会员:2000<积分<=10000;

                                   砖石会员:积分>10000;

代码如下:

package com.gy;

import java.util.Scanner;

public class VIPLevel {

	public static void main(String[] args) {
	//会员积分
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入会员积分:");
		int a = sc.nextInt();
		
		if(a<=500) {
			System.out.println("会员");
		}else if(a>500 && a<=2000) {
			System.out.println("白银会员");
		}else if(a>2000 && a<=10000) {
			System.out.println("黄金会员");
		}else if(a>10000) {
			System.out.println("砖石会员");
		}else {
			System.out.println("输入错误!");
		}
		
		
	}

}

        语句较为基础,在整理清楚语句的结构后,我们思考判断条件,整理清楚后编写相对简单,但我们作为新手,不能马虎;

        注意定义的数据类型,很多判断方法,例如,

                账号名的对比:

                                String name = sc.nextLine();索引录用。

                 对比应用:      

                                        name.equals("zhangsan") ;  

                                        即:"name"与"zhangsan"比较

本文为个人学习总结,感谢大家借鉴,共同进步!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@玥欧巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值