Java 基础 字符串相等

本文介绍了一个使用Java实现的简单案例,通过用户输入的成本、销售类型和税收类型来计算最终价格。涉及到了如何使用equals方法和charAt方法进行字符串比较。

字符串判断 是否相等 需要用 equals("需要判断的字符") 或 用 CharAt(0)== '需要判断的字符'

列子如下:

import java.util.*;

public class snippet
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println( "input cost : " );
        double cost = input.nextFloat();
        System.out.println( "input sale type : " );
        String sale_type = input.next();
        System.out.println( "input tax type : " );
        String tax_type = input.next();
        input.close();
    
        if ( sale_type.equals("s"))
            cost = cost - cost * 0.25 ;
        else
            System.out.println("No sale");
        
        if ( tax_type.charAt(0) == 'h')
            cost = cost + cost * 0.13 ;
        else if ( tax_type.contentEquals("g"))
            cost = cost + cost * 0.07 ;
        else
            System.out.println( "No tax" );
        
        System.out.println("Cost is " + cost + " sale_type is " + sale_type + " tax_type is " + tax_type);
        
    }

}

 

转载于:https://my.oschina.net/u/2275100/blog/840444

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值