本代码用if语句进行String判断,发现每次运行调用方法时,会自动跳过if 和 else if 中的语句,直接执行else 中语句,经过查询发现if对String型语句进行判断要用 equals来进行判断。目前本人对该类型语句不甚了解。等了解后再回头。
代码如下:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请你输入机票原价");
Double money=sc.nextDouble();
System.out.println("请你输入月份");
int month=sc.nextByte();
System.out.println("请您输入机票类型"+"("+"头等舱"+"或者"+"经济舱"+")");
String type = sc.next();
System.out.println("----------------");
System.out.println("您实际应付:");
Double pay=arr(money,month,type);
System.out.println(pay);
}
public static Double arr(Double money,int month,String type){
if (month<1||month>12){
return -1.0;
}else if (month>=5&&month<=10){
if (type=="头等舱"){
Double i = 0.9*money;
return i;
}else if (type=="经济舱") {
Double i=0.85*money;
return i;
}else {
return -1.0;
}
}else {
if (type=="头等舱"){
Double i = 0.7*money;
return i;
}else if (type=="经济舱"){
Double i=0.65*money;
return i;
}
else {
return -1.0;
}
}
}