Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java if和else语句_Java - Fatal编程技术网

Java if和else语句

Java if和else语句,java,Java,我是java新手,正在为一个角色而挣扎 所以我想在租车计划中设定一个新的价格 public class CarToBuy extends CAR { private int price; private int registration_Year; private int milage; private boolean sold; public CarToBuy(String Description, int cost, int year, int mi

我是java新手,正在为一个角色而挣扎 所以我想在租车计划中设定一个新的价格

public class CarToBuy extends CAR {
    private int price;
    private int registration_Year;
    private int milage;
    private boolean sold;

    public CarToBuy(String Description, int cost, int year, int miles) {
        super(Description);
        price = cost;
        registration_Year = year;
        milage = miles;
        sold = false;
    }

    public int getPrice() {
        return price;
    }

    public int getReg() {
        return registration_Year;
    }

    public int getMilage() {
        return milage;
    }

    public void setNewPrice(int newprice) {
        price = newprice;
        if (sold = false) {
            System.out.println("car not in stock");
        } else {
            System.out.println("The New price is" + newprice);
        }
    }

    public void carBuying(String Customer_name) {

        if (sold = false) {
            System.out.println("The Car is not in stock");
        } else {
            System.out.println("Thank you purchasing" + Customer_name);
        }
    }
}
即使用户收到一条显示消息“汽车没有库存”,价格也会得到更新,但是我想做的是,如果汽车没有库存,价格不会改变 所以如果false=价格不变
如果真实价格发生变化

,情况似乎是错误的。无需在if条件下比较布尔变量。 尝试更改以下代码

if (sold = false){
....
}


运算符=为变量赋值,而=用作条件运算符

所以,


如果sall为true,则执行第一个代码块,否则执行另一个代码块。

sall=false
应该是
sall==false
,如果(!sall)@Héctor,则更好。。我正试图纠正他的错误。当然,你是对的,我不想显得粗鲁…@anjum khan:请停止故意破坏你的问题。谢谢你,我这样做更有意义!现在明白了吗
if(!sold){
....
}
if(sold){
System.out.println("Thank you purchasing" + Customer_name);
} else {
System.out.println("The Car is not in stock")
}