目录
案例一: 键盘录入x的值,判断x的范围,执行不同的公式.
案例二:键盘录入月份的值,输出对应的季节
案例三:获取三个数据中的最大值
案例四:数的比较
案例一:
x和y的关系满足如下;
x>=3 y = 2x +1;
-1<=x<3 y = 2x;
x<=-1 y = 2x - 1;
键盘录入x的值,判断x的范围,执行不同的公式.
import java.util.Scanner;
public class IfTest01 {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//用变量x接收录入的int类型的数据
System.out.println("请输入x的值:");
int number = sc.nextInt();
int y = 0;
//if语句判断执行不同的路径
if (number >=3 ){
y = 2*number +1;
}else if (number>=-1 & number <3){
y=2*number;
} else if (number<=-1){
y = 2*number-1;
}else {
System.out.println("输入的数据有误");
}
System.out.println("y的值为:" + y);
}
}
案例二:
键盘录入月份的值,输出对应的季节
3-5 春季
6-8 夏季
9-11 秋季
12-2 冬季
import java.util.Scanner;
public class IfTest02 {
public static void main(String[] args) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//接收键盘录入的数据
System.out.println("请输入季节的数字1-12:");
int month = sc.nextInt();
if(3<=month & month<=5){
System.out.println("春季");
}else if (6<=month & month<=8){
System.out.println("夏季");
}else if (9<=month & month<=11){
System.out.println("秋季");
}else if (month==1){
System.out.println("冬季");
}else if (month==2){
System.out.println("冬季");
}else if (month==12){
System.out.println("冬季");
}else {
System.out.println("输入的数据有误");
}
}
}
案例三:
获取三个数据中的最大值
import java.util.Scanner;
public class IfTest03 {
public static void main(String[] args) {
// int a = 10;
// int b = 21;
// int c = 30;
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数据:");
int a = sc.nextInt();
System.out.println("请输入第二个数据:");
int b = sc.nextInt();
System.out.println("请输入第三个数据:");
int c = sc.nextInt();
if (b>c){
if (b>a){
System.out.println("最大值是b:"+ b);
}else{
System.out.println("最大值是a:"+ a);
}
}else {
if (c>a){
System.out.println("最大值是c:"+ c);
}else {
System.out.println("最大值是a:"+ a);
}
}
}
}
案例四:
需求1:
获取两个数中较大的数据
使用三目运算符解决
需求2:
获取 三个数中最大的数据
需求3:
比较两个数是否相同
public class OptTest1 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 40;
//三目运算符 需求1
int result = (a>b)?a:b;
System.out.println(result);
//三目运算符 需求2
int max = (result > c)?result:c;
System.out.println("三个数最大的数据:"+ max);
//三目运算符 需求3
int x = 11;
int y = 11;
boolean res2= (x == y)?true:false;
System.out.println("x与y进行比较 :" + res2);
}
}
获取三个数据中的最大值
import java.util.Scanner;
public class IfTest03 {
public static void main(String[] args) {
// int a = 10;
// int b = 21;
// int c = 30;
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数据:");
int a = sc.nextInt();
System.out.println("请输入第二个数据:");
int b = sc.nextInt();
System.out.println("请输入第三个数据:");
int c = sc.nextInt();
if (b>c){
if (b>a){
System.out.println("最大值是b:"+ b);
}else{
System.out.println("最大值是a:"+ a);
}
}else {
if (c>a){
System.out.println("最大值是c:"+ c);
}else {
System.out.println("最大值是a:"+ a);
}
}
}
}
案例四:
需求1:
获取两个数中较大的数据
使用三目运算符解决
需求2:
获取 三个数中最大的数据
需求3:
比较两个数是否相同
public class OptTest1 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 40;
//三目运算符 需求1
int result = (a>b)?a:b;
System.out.println(result);
//三目运算符 需求2
int max = (result > c)?result:c;
System.out.println("三个数最大的数据:"+ max);
//三目运算符 需求3
int x = 11;
int y = 11;
boolean res2= (x == y)?true:false;
System.out.println("x与y进行比较 :" + res2);
}
}
运行结果:
20
三个数最大的数据:40
x与y进行比较 :trueProcess finished with exit code 0
到底啦!你收获了些什么呢?



