我不知道为什么if语句在程序中不起作用。 当订单大于批量订单时,它仍然返回0。 您认为我编写的代码存在问题,如下所示?
public class ShoeStoreOrder{ //ShoeStoreOrder .java
// private method
private String typeofshoe ;
private String season ;
private double cost ;
private int bulkOrderQuantity;
private int discount;
private int order;
//constructor
public ShoeStoreOrder(String t,String s,double c,int b,int d,int o){
typeofshoe = t;
season = s;
cost = c;
bulkOrderQuantity = b;
discount = d;
order = o;
}
//get Method
public String gettypeofshoe(){
return typeofshoe;
}
public String getseason(){
return season;
}
public double getcost(){
return cost;
}
public int getbulkOrderQuantity(){
return bulkOrderQuantity;
}
public int getdiscount(){
return discount;
}
public int getorder(){
return order;
}
//set method
public void settypeofshoe(String t){
typeofshoe=t;
}
public void setseason(String s){
season=s;
}
public void setcost(double c){
cost=c;
}
public void setbulkOrderQuantity(int b){
bulkOrderQuantity=b;
}
public void setorder(int o){
order=o;
}
//other method
//overload method
public double gettotaldiscount(){
if(order()>bulkOrderQuantity()){
return order*cost*(discount/100);
}
else{
return 0;
}
}
public double gettotalamount(){
return order*cost-gettotaldiscount();
}
}
答案 0 :(得分:2)
order
和bulkOrderQuantity
是实例变量而不是方法。调用方法时使用()
,而不是引用变量时使用{<1}}:
if(order()>bulkOrderQuantity()){
将其更改为
if(order>bulkOrderQuantity){
答案 1 :(得分:0)
当订单值等于bulkOrderQuantity:
时,您跳过了大小写public double gettotaldiscount() {
if (order >= bulkOrderQuantity) {
return order * cost * discount / 100;
} else {
return 0;
}
}
//检查输入和结果:
public static void main(String[] args) {
ShoeStoreOrder sso;
System.out.println("No discount");
sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 1);
System.out.println(sso.gettotalamount());
System.out.println("After discount");
sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 5);
System.out.println(sso.gettotalamount());
sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 50);
System.out.println(sso.gettotalamount());
}
输出: