JAVA基础语法编程详解
来源:华佗健康网
5.计算商场折扣
题目描述
解题思路
思路一: 使用if...else()语句
if(price>=5000){
System.out.println((int)(price*0.6));
}else if(price>=2000){
System.out.println((int)(price*0.7));
}else if(price>=500){
System.out.println((int)(price*0.8));
}else if(price>=100){
System.out.println((int)(price*0.9));
} else{
System.out.println(price);
}
思路二: 使用switch...case()
switch (price / 100) {
case 0 :
f = 1.0f ;
break;
case 1:
case 2:
case 3:
case 4:
f = 0.9f;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19 :
f = 0.8f;
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 49 :
f = 0.7f;
break;
default :
f = 0.6f;
}
代码实现
思路一: 使用if...else()语句
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int price = console.nextInt();
int cost = 0;
//write your code here......
if (price >= 5000) {
cost = (int)(price * 0.6);
} else if (price >= 2000) {
cost = (int)(price * 0.7);
} else if (price >= 500) {
cost = (int)(price * 0.8);
} else if (price >= 100) {
cost = (int)(price * 0.9);
} else {
cost = (int)price;
}
System.out.println(cost);
}
}
思路二: 使用switch...case()
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int price = console.nextInt();
int cost = 0;
float f = 1.0f;
//price = (int) price/100;
//write your code here......
switch (price / 100) {
case 0 :
f = 1.0f ;
break;
case 1:
case 2:
case 3:
case 4:
f = 0.9f;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19 :
f = 0.8f;
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 49 :
f = 0.7f;
break;
default :
f = 0.6f;
}
cost = (int) Math.floor(price * f);
// cost = (int) cost;
System.out.println(cost);
}
}
总结
if…else()语句语法详解
在程序中,if else语句是这样子使用的:
if (条件表达式)
{
执行语句1;
}else{
执行语句2;
}
if后边小括号里是判断条件是否满足,如果满足则执行语句1,不满住则执行语句2;
- if-else语句的嵌套
if-else语句还可以嵌套使用,以实现更复杂的条件判断。例如,我们可以使用嵌套的if-else语句来判断一个数是否为偶数:
num = int(input("请输入一个整数:"));
if num % 2 == 0
{
if num == 0
{
print("这是零");
}else{
print("这是一个偶数");
}else{
print("这是一个奇数");
}
}
- if-else语句的注意事项
if-else语句中的条件表达式必须返回一个布尔值(True或False)。
if-else语句中的代码块必须缩进,通常使用4个空格或一个制表符进行缩进。
else语句是可选的,可以省略。
switch…case()语句语法详解
switch-case语句格式如下
switch(变量){
case 变量值1:
//;
break;
case 变量值2:
//...;
break;
...
case default:
//...;
break;
}
swtich()变量类型只能是int、short、char、byte和enum类型(JDK 1.7 之后,类型也可以是String了)。当进行case判断时,JVM会自动从上到小扫描,寻找匹配的case,可能存在以下情况:
- 若未找到,则执行默认的case。
int i = 5;
switch(i){
case 0:
System.out.println("0");break;
case 1:
System.out.println("1");break;
case 2:
System.out.println("2");break;
default:
System.out.println("default");break;
}
输出:default
- 当每一个case都不存在break时,JVM并不会顺序输出每一个case对应的返回值,而是继续匹配,匹配不成功则返回默认case。
int i = 5;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
输出:default
- 当每一个case都不存在break时,匹配成功后,从当前case开始,依次返回后续所有case的返回值。
int i = 2;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
输出:2
default
- 若当前匹配成功的case不存在break,则从当前case开始,依次返回后续case的返回值,直到遇到break,跳出判断。
int i = 2;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3");break;
default:
System.out.println("default");
}
输出:2 3
因此switch case执行时,一定会先进行匹配,匹配成功返回当前case的值,再根据是否有break,判断是否继续输出,或是跳出判断。
还需注意的是case后面只能是常量,可以是运算表达式,但一定要符合正确的类型。不能是变量,即便变量在之前进行了赋值,JVM依然会报错。
因篇幅问题不能全部显示,请点此查看更多更全内容