感谢您提前提供的帮助。请注意,我是初学者。下面是代码。它只打印else语句值为"0";感谢您与我们一起购买";即使if语句值应该打印到控制台?
class Main {
String type;
double price;
boolean order;
public Main(String whatPizza, double costOfPizza, boolean yourOrder){
if (price > 10.00){
System.out.println("Thank you for spending over 10 pounds with us!");
} else {
System.out.println("Thank you for buying with us!");
}
type = whatPizza;
price = costOfPizza;
order = yourOrder;
}
public static void main(String[] args) {
//empty for now
Main personA = new Main("Pepperoni", 11.00, true);
Main personB = new Main("Cheese", 9.00, true);
}
}
您的代码顺序不对。运行if语句时,没有为price分配任何值。现在双变量价格总是空的,所以它永远不会大于10.00,因此是假的,并返回其他值。这应该会奏效:
class Main {
String type;
double price;
boolean order;
public Main(String whatPizza, double costOfPizza, boolean yourOrder){
type = whatPizza;
price = costOfPizza;
order = yourOrder
if (price > 10.00){
System.out.println("Thank you for spending over 10 pounds with us!");
} else {
System.out.println("Thank you for buying with us!");
}
;
}
public static void main(String[] args) {
//empty for now
Main personA = new Main("Pepperoni", 11.00, true);
Main personB = new Main("Cheese", 9.00, true);
}
}