目录
前言
一、基本结构
1.if语句
2.switch case语句
二、选择语句应用实例
1.判断闰年
2.判断奇偶数
3.小型计算机系统
总结
前言
选择结构用于判断给定的条件,根据判断的结果判断某些条件,根据判断的结果来控制程序的流程。今天,让我们一起学习if和switch两种选择语句及实例吧!
一、基本结构
1.if语句
(1)if(表达式){
语句一
}
public void test() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a:");
String a = sc.nextLine();
char s = a.charAt(0);//取出字符串的第一个字母
if(s<='0' && s>='9') {
System.out.println(s+"是数字");
}if(s>='A' && s<='Z') {
System.out.println(s+"是大写字母");
}if(s>='a' && s<='z') {
System.out.println(s+"是小写字母");
}if(s>='\u4e00' && s<='\u9fa5') { //utf-8的中文编码范围
System.out.println(s+"是中文");
}
}
(2) if(表达式1){
语句一
}else if(表达式2){
语句二
}else{
语句三
}
public void test1() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a:");
String a = sc.nextLine();
if(a.equals(" ")|| a == null) {
System.out.println("输入为空...");
return ; //返回,结束程序
}
char s = a.charAt(0);//取出字符串的第一个字母
if(s<='0' && s>='9') {
System.out.println(s+"是数字");
}else if(s>='A' && s<='Z') {
System.out.println(s+"是大写字母");
}else if(s>='a' && s<='z') {
System.out.println(s+"是小写字母");
}else if(s>='\u4e00' && s<='\u9fa5') { //utf-8的中文编码范围
System.out.println(s+"是中文");
}else {
System.out.println(s+"是其他字符");
}
}
2.switch case语句
switch(表达式){
case 1:
break;
case2:
break;
....
default:
break;
}
public void test2() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入(周一~周天):");
String s = sc.nextLine();
switch(s) {
case "周一":
System.out.println("开例会!");
break;
case "周二":
System.out.println("项目分析!");
break;
case "周三":
System.out.println("项目开发!");
break;
case "周四":
System.out.println("项目测试!");
break;
case "周五":
System.out.println("项目验收!");
break;
case "周六"://越界(周六周天干同一件事)
case "周天":
System.out.println("休息!");
break;
default:
System.out.println("输入不合法...");
break;
}
}
二、选择语句应用实例
1.判断闰年
public void isLeap() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = Integer.parseInt(sc.nextLine());
if((year%400==0)||(year%4==0 && year%100!=0)) {
System.out.println(year+"是闰年");
}else {
System.out.println(year+"不是闰年");
}
}
2.判断奇偶数
public void EvenTest() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字a:");
int a = Integer.parseInt(sc.nextLine());
if(a%2==0) {
System.out.println(a+"是偶数");
}else {
System.out.println(a+"是奇数");
}
}
3.小型计算机系统
public void demo1() {
Scanner sc = new Scanner(System.in);
final String PLUS = "1";
final String SUB = "2";
final String MUL = "3";
final String DIVI = "4";
final String POW = "5";
final String MOD = "6";
final String SQRT = "7";
final String EVEN = "8";
final String MAX = "9";
final String SORT_MAX_TO_MIN = "10";
final String EXIT = "0";
final int EVEN_FLAG = 2;
int a = 0,b = 0;
double x = 0,y =0 ;
System.out.println("-------------------------------");
System.out.println("1.加法\t 2.减 法\t ");
System.out.println("3.乘法\t 4.除 法\t ");
System.out.println("5.幂 \t 6.取模\t ");
System.out.println("7.开 方\t 8.是否为偶数\t ");
System.out.println("9.最大值\t 10.从大至小排序\t ");
System.out.println("0.退出");
System.out.println("-------------------------------");
System.out.println("请选择运算方式");
String choice = sc.nextLine();
switch (choice) {
case EXIT:
char s = choice.charAt(0);//取出字符串的第一个字母
if(s == 0) {
System.out.println("已退出");
//return ;
}
break;
case PLUS:
System.out.println("请输入数字a:");
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入数字b:");
b = Integer.parseInt(sc.nextLine());
int sum = a+b;
System.out.println(a+"+"+b+"="+ sum);
break;
case SUB:
System.out.println("请输入数字a:");
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入数字b:");
b = Integer.parseInt(sc.nextLine());
int sub = a-b;
System.out.println(a+"-"+b+"="+ sub);
break;
case MUL:
System.out.println("请输入数字am:");
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入数字b:");
b = Integer.parseInt(sc.nextLine());
int mul = a*b;
System.out.println(a+"*"+b+"="+ mul);
break;
case DIVI:
System.out.println("请输入除数x:");
x = Double.parseDouble(sc.nextLine());
System.out.println("请输入被除数y:");
y = Double.parseDouble(sc.nextLine());
if(y != 0) {
double d = x / y;
System.out.println(x+"➗"+y+"="+ d);
}else {
System.out.println("被除数不能为0");
}
break;
case POW:
System.out.println("请输入数字x:");
x = Double.parseDouble(sc.nextLine());
System.out.println("请输入数字y:");
y = Double.parseDouble(sc.nextLine());
int result = (int)Math.pow(x, y);
System.out.println(x+"^"+y+"="+result);
break;
case MOD:
System.out.println("请输入数字x:");
x = Double.parseDouble(sc.nextLine());
System.out.println("请输入数字y:");
y = Double.parseDouble(sc.nextLine());
double result1 = x%y;
System.out.println(x+"%"+y+"="+result1);
break;
case SQRT:
System.out.println("请输入数字x:");
x = Double.parseDouble(sc.nextLine());
double result2 = Math.sqrt(x);
System.out.println(x+"开方的结果:"+result2);
break;
case EVEN:
System.out.println("请输入数字a:");
a = Integer.parseInt(sc.nextLine());
if(a%EVEN_FLAG == 0) {
System.out.println(a+"是偶数");
}else {
System.out.println(a+"不是偶数");
}
break;
case MAX:
System.out.println("请输入数字a:");
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入数字b:");
b = Integer.parseInt(sc.nextLine());
//int max = Math.max(a, b);
System.out.println(a+"和"+b+"之间的最大值为:"+Math.max(a, b));
break;
case SORT_MAX_TO_MIN:
System.out.println("请输入数字a:");
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入数字b:");
b = Integer.parseInt(sc.nextLine());
int max = Math.max(a, b);
int min = Math.min(a, b);
System.out.println("由大到小排序为:"+max+" ,"+min);
break;
default:
System.out.println("输入不合法!");
break;
}
}
运行结果部分截图:
总结
本文对if和switch两种选择进行基本结构和实例应用的介绍,希望大家在遇到分支型问题时能对选择结构加以应用。本人才疏学浅,有问题请大家多多指教!