import java.util.Scanner;
class Test1_If {
public static void main(String[] args) {
/*
练习:键盘录入成绩,判断优良
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生成绩范围在1到100之间");
int x = sc .nextInt();
if (x >=90 && x <= 100) {
System.out.println("优");
}else if (x >= 80 && x <=89) {
System.out.println("良");
}else if (x >= 70 && x <=79) {
System.out.println("中");
}else if (x >= 60 && x <=69) {
System.out.println("及");
}else if (x >=0 && x <=59) {
System.out.println("差");
}else
System.out.println("成绩录入错误");
}
}
if语句的小练习
import java.util.Scanner;
class Test1_If {
public static void main(String[] args) {
/*
练习:键盘录入成绩,判断优良
*/
Scanner sc = new Scanner(System.in);
/*System.out.println("请输入学生成绩范围在1到100之间");
int x = sc .nextInt();
if (x >=90 && x <= 100) {
System.out.println("优");
}else if (x >= 80 && x <=89) {
System.out.println("良");
}else if (x >= 70 && x <=79) {
System.out.println("中");
}else if (x >= 60 && x <=69) {
System.out.println("及");
}else if (x >=0 && x <=59) {
System.out.println("差");
}else
System.out.println("成绩录入错误");
System.out.println();
*/
System.out.println("请输入第一个整数");
int x = sc.nextInt();
int y =0;
if (x>= 3) {
y=2*x+1;
}else if (x>=-1 && x<3) {
y =2*x;
}else if (x<=-1) {
y =2*x-1;
}
System.out.println(y);
}
}
用if语句嵌套比较三个数的大小
class Demo6_IfIf {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;
if (a>b) {
if (a>c) {
System.out.println(a+"是最大值");
}else {
System.out.println(c+"是最大值");
}
}else {
if (b > c) {
System.out.println(b+"是最大值");
}else {
System.out.println(c+"是最大值");
}
}
}
}
switch语句格式:
class Demo_Switch {
/*seitch语句格式
switch(表达式){ 可以接受byte,short,,char,int
case 值一; 引用数据类型可以接受枚举,string字符串
语句体1;
break;
case 值2;
break;
。。。。
default ;
语句体n+1
}
*/
public static void main(String[] args) {
String name ="rose";
String gender ="跨性别认知者";
switch (gender) {
case "男士":
System.out.println(name + "是一位" + gender + "喜欢抽烟喝酒抠脚");
break;
case "女士":
System.out.println(name + "是一位" + gender + "喜欢逛街看剧看小说");
break;
default:
System.out.println(name + "是一位" + gender + "他战胜世俗眼光做自己");
break;
}
}
}
switch语句的小练习
class Test1_Switch {
public static void main(String[] args) {
//给出一个值,输出对应的星期几
int week =5;
switch (week) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
default:
System.out.println("输入无效数字");
}
}
}
switch语句注意事项:
case后面只能是常量,不能是变量,多个case后面的值不能出现相同
default可以省略但一般不建议省略。
break,最后一个可以省略,其他不可以省略,省略可能导致case穿透的情况;
case后面只能放byte short char int 以及string字符串和enum枚举
default可以放在任意位置,但是一般放在最后,不管放在哪里都是最后执行。
switch的结束条件
1.遇到break
2.遇到右大括号
switch语句if语句适用范围
if建议在某个区间做判断
switch建议对固定值做判断
import java.util.Scanner;
class Test3_Switch {
public static void main(String[] args) {
/*一年有四季,
345对应春季,
678夏季
9.10.11秋季
12.1.2冬季
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份");
int month = sc.nextInt();
/*switch (month) {
case 3:
case 4:
case 5:
System.out.println(month+"月是春天");
break;
case 6:
case 7:
case 8:
System.out.println(month+"月是夏天");
break;
case 9:
case 10:
case 11:
System.out.println(month+"月是秋天");
break;
case 12:
case 1:
case 2:
System.out.println(month+"月是冬天");
break;
default:
System.out.println("对8起,没有对应的季节");
*/
if(month>12 || month<1){
System.out.println("对不起没有对应的季节");
}else if(month>=3 && month <=5){
System.out.println(month+"月是春季");
}else if(month>=6 && month<=8) {
System.out.println(month+"月是夏季");
}else if(month>=9 && month<=11){
System.out.println(month+"月是秋季");
}else {
System.out.println(month+"月是冬季");
}
}
}