if对String进行判断

        本代码用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;
            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值