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"比较
本文为个人学习总结,感谢大家借鉴,共同进步!