gpt4 book ai didi

java - 接受输入、进行计算并显示答案

转载 作者:行者123 更新时间:2023-12-01 11:26:23 26 4
gpt4 key购买 nike

所以我正在尝试用java编写一个基本程序。它接受输入,对输入进行计算并显示结果。它有 3 个构造函数,第一个构造函数不接受输入并创建错误消息,第二个和第三个构造函数接受输入并对其执行计算。

所以程序应该接受(来自创建者类):

订单o1 = new Order("车牌号码", 12.7, 6)

程序确实接受上述代码,但不执行任何计算,仅显示“新订单”。

不确定我在这里做错了什么,我添加了评论来尝试解释我在做什么。

public class Order {
private static int orderNum = 0;
public Order(){
orderNum++; //setting the static variable
}

private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

//setting the instance variables and booleans


public boolean isDiscounted(){ //setting the valid
discount boolean value
if (discount > 100 || discount ==0) {
return false;
}
else {
return true;
}
}

public boolean isValidOrder(){ //setting the valid order boolean value
if (total ==0){
return false;
}
else {
return true;
}
}

public Order(String m) {
orderNum++;
this.message = m;
isValidOrder = false;
isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set

public Order(String pN, double p, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.quantity = q;
isValidOrder = true;
isDiscounted = false;
}
//second constructor accepting 3 parameters, with boolean values set

public Order(String pN, double p, double d, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.discount = d;
this.quantity = q;
isValidOrder = true;
isDiscounted = true;
}
//third constructor accepting 4 parameters, with boolean values set

public void setTotal(double t){
total = t;
isDiscounted = true;
}

//setting the total



public double getTotal(){
if (isValidOrder == true){
return ((price * quantity)-(price * quantity * (discount/100)));
}
else if (isDiscounted == false){
return (price * quantity);
}
return total;
}
//getting the total and calculations



@Override
public String toString(){
if (isDiscounted == true){
message = ("Order number: " + Order.orderNum + "\nProduct
Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:
" + quantity
+ "\nDiscount: " + discount + "\nTotal Price: " + total);
}

//first part of toString method, with discount
else if (isDiscounted == false){
message = ("Order number: " + Order.orderNum + "\nProduct
Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:
" + quantity
+ "\nTotal Price: " + total);
}
//second part of toString method, with no discount
else if (isValidOrder == false){
message = "Error";
}
//third part of toString method with error message
return message;
}

}

最佳答案

我修改了你的代码,检查一下。

public class Order {
private static int orderNum = 0;
public Order(){
orderNum++; //setting the static variable
}
private String productName;
public double price;
private double discount;
public double quantity;
public double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;

//setting the instance variables and booleans


public boolean isDiscounted() { //setting the discount boolean
if (discount > 100 || discount == 0) {
return false;
} else {
return true;
}
}

public boolean isValidOrder() { //setting the valid order boolean value
if (total == 0) {
return false;
} else {
return true;
}
}

public Order(String m) {
orderNum++;
this.message = m;
isValidOrder = false;
isDiscounted = false;
}
//first constructor accepting no parameters, with boolean values set

public Order(String pN, double p, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.quantity = q;
isValidOrder = true;
isDiscounted = false;
}
//second constructor accepting 3 parameters, with boolean values set

public Order(String pN, double p, double d, int q) {
orderNum++;
this.productName = pN;
this.price = p;
this.discount = d;
this.quantity = q;
isValidOrder = true;
isDiscounted = true;
}
//third constructor accepting 4 parameters, with boolean values set

public void setTotal(double t) {
total = t;
isDiscounted = true;
}

//setting the total


public double getTotal() {
if (isValidOrder == true) {
return ((price * quantity) - (price * quantity * (discount / 100)));
} else if (isDiscounted == false) {
return (price * quantity);
}
return total;
}
//getting the total and calculations


@Override
public String toString() {
if (isDiscounted == true) {
message = ("Order number: " + Order.orderNum + " \n Product Name: "
+ productName + "\nProduct Price: " + price + "\nOrder Quantity: " + quantity +
"\nDiscount: " + discount + "\nTotal Price: " + total);
}

//first part of toString method, with discount
else if (isDiscounted == false) {
message = ("Order number: " + Order.orderNum + "\nProduct Name: " + productName
+ "\nProduct Price: " + price + "\nOrder Quantity:" + quantity
+ "\nTotal Price: " + total);
}
//second part of toString method, with no discount
else if (isValidOrder == false) {
message = "Error";
}
//third part of toString method with error message
return message;
}

}

关于java - 接受输入、进行计算并显示答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30771850/

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