close

{買賣物品折扣問題}

[主程式]

public class hey
{
    public static void main(String[] args)
    {
        Product aaa = new Product();
        Product bbb = new Product();
        
        aaa.name = "bottle";
        aaa.oringinalPrice = 10;
        aaa.setDiscount(-1);
        
        bbb.name = "cookie";
        bbb.oringinalPrice = 30;
        bbb.setDiscount(-1);
        
        if(aaa.getPrice()< bbb.getPrice())
        {
            System.out.println("A is cheaper");
        }
        else
        {
            System.out.println("B is cheaper");
        }
        aaa.print();
        bbb.print();
    }
}
 

[宣告類別-封裝]

public class Product 
{
    public String name;
    public int oringinalPrice;
    private int discount;
    
    public Product()
    {
        name = "???";
        oringinalPrice = 0;
        discount = 10; 
    }
    
    public float getPrice()
    {
        return oringinalPrice * (discount / 10.0f);
    }
    
    public void setDiscount(int inputDiscount)
    {
        if(inputDiscount >= 0 && inputDiscount <= 10)
        {
            discount = inputDiscount;
        }
        else 
        {
            System.out.println("invalid discount!!!");
        }
    }
    
    public void print()
    {
        System.out.println("name:" + name +", price:" + getPrice());
    }
}
 

{continue - 迴圈控制 }

[我已經知道5 / 2是多少,假設多算一次要1HR,所以不要讓他算]

public class hello
{
    public static void main(String[] args)
    {
        for(int i = 0; i < 10; i++)
        {
            if(i == 5)
            {
                continue;
            }
            System.out.println(i / 2);
        }
    }
}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 她家 的頭像
    她家

    她家

    她家 發表在 痞客邦 留言(0) 人氣()