gpt4 book ai didi

java - 无法显示计算结果

转载 作者:行者123 更新时间:2023-12-01 09:54:24 25 4
gpt4 key购买 nike

所以我有一个项目需要创建这个:

 public void calculate (){
if (isDiscounted == true){
total = quantity * price - quantity * price * (discount/100);
}
else {
total = quantity * price;
}
}

但是这里的总数没有正确显示,当我将其设置为 ("Compass", 20 , 30) 时,折扣不适用,仅适用正常总数:

 public String getOrderDetails(){
message = message;
if(isValidOrder == true && isDiscounted == false){
message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
}
else if(isValidOrder == true && isDiscounted == true){
message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" +"Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
}
else {
return message;
}
return message;
}

我是一个非常初级的程序员,所以我不确定你是否明白我在说什么。基本上我无法链接它:/

这是其余的代码:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package order;
import java.util.Arrays;
/**
*
* @author Alexander
*/
public class Order {
private static String[] products = {
"Compass", "Eraser", "Pen", "Pencil", "Pencil Case", "Pencil Sharpener", "Ruler", "Scissors"
};
private static double[] prices = {
4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5
};
public static int orderNum = 0; //
private String productName;
private double price = 0;
private int discount = 0;
private final int minDiscount = 0;
private final int maxDiscount = 50;
private int quantity = 0;
private final int minQuantity = 0;
private final int maxQuantity = 1000;
private double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

public void calculate() {
if (isDiscounted == true) {
total = quantity * price - quantity * price * (discount / 100);
} else {
total = quantity * price;
}
}


public Order() {
isValidOrder = false;
message = "**ERROR** : Order number cannot be totalled as no details have been supplied.";
orderNum++;
}

public Order(String productName, int quantity) {
this.quantity = quantity;
this.productName = productName;
testQuantity(quantity);
getPrice(productName);
if (isValidOrder == true) {
calculate();
}
orderNum++;
}

public Order(String productName, int quantity, int discount) {
this.productName = productName;
this.quantity = quantity;
this.discount = discount;
testQuantity(quantity);
testDiscount(discount);
getPrice(productName);
if (isValidOrder != false) {
calculate();
}
orderNum++;
}



private void getPrice(String pce) {
Arrays.sort(products);
int searchProductArray = Arrays.binarySearch(products, pce);
if (searchProductArray >= 0) {
price = prices[searchProductArray];
productName = products[searchProductArray];
isValidOrder = true;
} else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}




public void testQuantity(int quantity) {
boolean isValidOrder = true;
if (quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
} else if (quantity > maxQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
} else {
this.quantity = quantity;
this.isValidOrder = true;
}
}

public void testDiscount(int discount) {
boolean isDiscounted = false;
if (discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
} else if (discount > maxDiscount) {
message = "**ERROR**: The discount rate cannot be greater than 50";
} else {
this.discount = discount;
this.isDiscounted = true;
}
}

public String getOrderDetails() {
message = message;
if (isValidOrder == true && isDiscounted == false) {
message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
} else if (isValidOrder == true && isDiscounted == true) {
message = "Order Number: " + quantity + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Discount : " + discount + "%" + "\n" + "Total Price: $" + total;
} else {
return message;
}
return message;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Order O1 = new Order("Compass", 20, 30);




System.out.println(O1.total);

OrderLaunch frame = new OrderLaunch();
frame.setVisible(true);
}
}

最佳答案

整数除法!

这一行

total = quantity * price - quantity * price * (discount / 100);

折扣除以100。两者都是整数,因此如果折扣低于100,则计算结果为0。

您只需将其设为双重操作即可:

total = quantity * price - quantity * price * (discount / 100.0);

关于java - 无法显示计算结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com