第三章__if与switch选择结构
1.if的循环结构:
单分支: if(){ }
双分支: if(){ }else{ }
例:
import java.util.Scanner;
public class Demo1{
/** * 0.0 * * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("用户名为:");
char name = input.next().charAt(0);
System.out.println("密码为:");
int pwd = input.nextInt(); char qing = '青'; int mima = 123;
if (name == qing && pwd == mima)
{ System.out.println("欢迎你" + name); }
else { System.out.println("对不起,你不是青"); } }
}
多分支: 分段比较输出 90 80 60 不及格
if(条件1){
//条件为真 }
else if(条件2){ 条件1为假,条件2为真 }
else if(条件3){ 条件1、2为假 }else{ 条件都为假 }
例:
package cn.jbit.myshopping;
import java.util.Scanner;
public class CalcDiscount2 {/** * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入会员积分:");
int score = input.nextInt(); double discount;
if (score > 8000) { discount = 0.6; System.out.println("该会员享受的是6折");
} else if (score > 4000) { discount = 0.7; System.out.println("该会员享受的是7折");
} else if (score > 2000) { discount = 0.8; System.out.println("该会员享受的是8折");
} else { discount = 0.9; System.out.println("该会员享受的是9折"); } }
}
嵌套if: if(条件1){
if(条件2)
{ //条件1、2为真 } }
else{ //条件都为假 }
}
例:
package cn.jbit.myshopping;
import java.util.Scanner;
public class CalcDiscount {
/** * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入是否是会员:是(y)否(其他字符)");
String zifu = input.next(); System.out.println("请输入购物金额:");
double money = input.nextDouble();
double discount; if (zifu.equals("y")) { if (money > 200) { discount = 0.75; money = money * discount;
} else { discount = 0.8; money = money * discount; }
} else
{ if (money > 100) { discount = 0.9; money = money * discount; }
} System.out.println("实际支付:" + money);
}
}
2.switch选择结构
switch(表达式){//一定要先看是否有匹配的case项,如果有,执行相应的case内容,没有则执行default
//无论执行的是case还是default ,只要没有break,就会从下向上的运行。
case 常量1: //case后面的值不能重复,数据类型和case的值要想匹配,
//代码块
break;
…………
default:
//代码块
break;
}
注意:switch在JDK1.7以后,可以引用String类型 不能够引用double和long.
例1: 关于用switch的选择语句
import java.util.Scanner;
public class LoginMain {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
System.out.println("\t欢迎使用我行我素购物管理系统\n");
System.out.println("\t\t1.登录系统\n");
System.out.println("\t\t2.退出"); System.out.println("*****************************\n\n");
System.out.println("请选择,输入数字:");
Scanner input = new Scanner(System.in); int choose1 = input.nextInt();
switch (choose1) { case 1: System.out.println("1.客户信息管理");
System.out.println("2.购物结算");
System.out.println("3.真情回馈");
System.out.println("4.注销");
System.out.println("请选择,输入数字:");
int choose2 = input.nextInt(); switch (choose2) {
case 1: System.out.println("购物管理系统>客户信息管理");
System.out.println("1.显示所以客户信息");
System.out.println("2.添加客户信息");
System.out.println("3.修改客户信息");
System.out.println("4.查询客户信息");
break;
case 3: System.out.println("购物管理系统>真情回馈");
System.out.println("1.幸运大放送");
System.out.println("2.幸运抽奖");
System.out.println("3.生日问候");
break;
default:
System.out.println("您的输入有误");
break; }
break;
case 2: System.out.println("谢谢您的使用");
break;
default: System.out.println("您的输入有误");
break; }
}
}
3.switch语句与if语句的嵌套使用
例1:
import java.util.Scanner;
public class TravelUp {
/** * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
double disWangTou = 0.9; double disWangJing = 0.8; double disDanTou = 0.5; double disDanJing = 0.4; double cost = 5000;
Scanner input = new Scanner(System.in);
System.out.println("请输入您出行的月份:");
int month = input.nextInt();
if (month < 1 || month > 12) { System.out.println("您输入的月份有误"); }
else { switch (month) { // 旺季
case 4: case 5: case 6: case 7: case 8: case 9: case 10: System.out.println("请问您选择头等舱还是经济舱?");
int choose = input.nextInt();
if (choose == 1) {// 旺季头等舱
System.out.println("您的机票价格为" + cost * disWangTou);
} else if (choose == 2) {// 旺季经济舱
System.out.println("您的机票价格为:" + cost * disWangJing);
} else { System.out.println("您的输入有误"); }
break;
case 1: case 2: case 3: case 11: case 12: System.out.println("请问您选择头等舱还是经济舱?");
int choose1 = input.nextInt(); if (choose1 == 1) {// 淡季头等舱
System.out.println("您的机票价格为" + cost * disDanTou);
} else if (choose1 == 2) {// 淡季经济舱
System.out.println("您的机票价格为:" + cost * disDanJing);
} else { System.out.println("您的输入有误"); }
break;// 淡季 }
}
}
}
例2: 迷你计算器
import java.util.Scanner;
public class Conclusion {
/** * @param args */
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个操作数:");
if (input.hasNextDouble() == true) {
double num1 = input.nextDouble();
System.out.println("请输入第一个操作数:");
if (input.hasNextDouble() == true) {
double num2 = input.nextDouble(); System.out.println("请输入运算符号:");
String symbol = input.next();
switch (symbol) { case "+": System.out.println("计算结果为:" + (num1 + num2));
break;
case "-":
System.out.println("计算结果为:" + (num1 - num2));
break;
case "*":
System.out.println("计算结果为:" + (num1 * num2));
break;
case "/":
System.out.println("计算结果为:" + (num1 / num2));
break;
default:
System.out.println("您的输入有误");
break; } // switch的括号
} else { /* 嵌套if的else */
System.out.println("您第二个数输入有误"); }
}// 外层if的括号 else {
System.out.println("您第一个数输入有误"); }
}
}
此代码中 运用了input.hasNextDouble()==true
如果输入的不是数字 而是例如字母a b c ,则会报错,这就是输入非法字符引起的系统异常.因此需要运用input.hasNextDouble()==true来保证运行时 输出的都是数字。否则运行不成功。