java嵌套if选择结构问题

2025-02-27 23:57:47
推荐回答(2个)
回答1:

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
double price1 = 0;
double price2 = 0;

if((month > 0 && month < 4) || (month > 10 && month < 13) ){
price1 = 5000 * 0.5;
price2 = 5000 * 0.4;
}else if(month >= 4 && month <= 10){
price1 = 5000 * 0.9;
price2 = 5000 * 0.8;
}else{
System.out.println("输入有误!");
}
System.out.println("price1:" + price1);
System.out.println("price2:" + price2);
}

}

回答2:

代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

try {

float price = 5000;
float result = 0;
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入月份:");
String str_month = strin.readLine();
System.out.print("请输入仓位:");
String str_cangwei = strin.readLine();
//月份转换为int型
int month = Integer.parseInt(str_month);

if(month<=10&&month>=4){
if(str_cangwei.equals("经济舱")){
result = (float) (price*0.8);
}else{
result = (float) (price*0.9);
}
}else{
if(str_cangwei.equals("经济舱")){
result = (float) (price*0.4);
}else{
result = (float) (price*0.5);
}
}
System.out.print("价位:"+result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------------------------------
输出结果:

请输入月份:3
请输入仓位:经济舱
价位:2000.0