理解java条件

理解java条件,java,conditional,Java,Conditional,我正在学习Java,我正在努力理解为什么我正在编写的一个简单程序没有按应有的方式工作 import java.util.Scanner; class CarApp{ String carMake; String carColour; String features; int carPrice; void carFinal(){ System.out.println(carMake); System.out.println(carPrice); if(carMake =

我正在学习Java,我正在努力理解为什么我正在编写的一个简单程序没有按应有的方式工作

import java.util.Scanner;

class CarApp{
String carMake;
String carColour;
String features;
int carPrice;

void carFinal(){
    System.out.println(carMake);
    System.out.println(carPrice);
    if(carMake == "ford")
    {
        carPrice = 120000;
    }
    else if(carMake == "porsche")
    {
        carPrice = 1000000;
    }
    System.out.println(carPrice);
    System.out.println("Thank you for choosing your car with the car chooser app!" + "\n");
    System.out.println("You have chosen a " + carColour + " " + carMake + " with " + features + "\n" );
    System.out.println("Your car will be delivered to you in 7 working days. At a price of R" + carPrice + ".");
}
}

public class App {
public static void main(String[] args) {
    Scanner carChooser = new Scanner(System.in);

    CarApp carApp = new CarApp();
    System.out.println("Please let us know which car you would like, porsche or ford:");
    carApp.carMake = carChooser.nextLine();
    System.out.println("Please say which color car you would like:");
    carApp.carColour = carChooser.nextLine();
    System.out.println("Which features would you like added to your car:");
    carApp.features = carChooser.nextLine();
    carApp.carFinal();

}
}
系统似乎没有打印价格?所以我总是得到以下信息:

Your car will be delivered to you in 7 working days. At a price of R0.
任何帮助都将不胜感激,我相信这是一件非常琐碎的事情,也许是我忽略了的事情。提前感谢,


Fred在比较应该使用的字符串时

例如:

if(carMake.equals("ford"))
{
    carPrice = 120000;
}
else if(carMake.equals("porsche"))
{
    carPrice = 1000000;
}
另外两项说明:

对于这个用例,您可能希望使用检查相等性的,忽略该用例。例如,无论用户是否输入了ford、ford、ford、ford等,这都会触发第一个案例。 一些开发人员更喜欢使用约定,这会导致编写条件语句,就像if ford.equalscarMake一样。当与.equals等实例方法一起使用时,它可以防止细微的NullPointerException泄漏到代码中。与==运算符一起使用时,它可以防止意外赋值。
比较字符串时,应使用

例如:

if(carMake.equals("ford"))
{
    carPrice = 120000;
}
else if(carMake.equals("porsche"))
{
    carPrice = 1000000;
}
另外两项说明:

对于这个用例,您可能希望使用检查相等性的,忽略该用例。例如,无论用户是否输入了ford、ford、ford、ford等,这都会触发第一个案例。 一些开发人员更喜欢使用约定,这会导致编写条件语句,就像if ford.equalscarMake一样。当与.equals等实例方法一起使用时,它可以防止细微的NullPointerException泄漏到代码中。与==运算符一起使用时,它可以防止意外赋值。
@RohitJain在这一点上,我认为所有带有Java标记的第一个问题中约有10%涉及错误地比较字符串。我们需要一个公共服务公告。@RohitJain在这一点上,我认为所有带有Java标记的第一个问题中约有10%涉及错误地比较字符串。我们需要一个公共服务公告。谢谢你,乔纳森。来自脚本语言,这有点奇怪。现在说得通了,谢谢你,乔纳森。来自脚本语言,这有点奇怪。现在有道理了。