gpt4 book ai didi

java - 无法弄清楚如何打印 toString

转载 作者:行者123 更新时间:2023-12-02 03:54:27 25 4
gpt4 key购买 nike

package homeWork;

public class ShoppingBag {

private int items;
private float totalRetailCost;
private float taxRate;

public ShoppingBag(float taxRate){
this.taxRate = taxRate;
items = 0;
totalRetailCost = 0.0f;
}

// Transformer
public void place(int numItems, float theCost){
items = items += numItems;
totalRetailCost += (numItems * theCost);
}

public int getItems(){
return items;
}

public float getRetailCost(){
return totalRetailCost;
}

public float getTotalCost(){
return totalRetailCost + (1 + taxRate);
}

public String toString(){
String result = "The bag contains " + items + " items";
result += "The retail cost of items is" + totalRetailCost;
result += "The total cost = " + getTotalCost();
return result;

}
}

package homeWork;

import java.util.*;

public class MainClass {

public static void main(String[] args){
Scanner conIn = new Scanner(System.in);
ShoppingBag sb = new ShoppingBag(0.06f);
int count = 0;
float cost = 0.0f;
System.out.print("Enter count (0 to stop):");
count = conIn.nextInt();

while(count != 0){
System.out.print("Enter cost: ");
cost = conIn.nextFloat();
sb.place(count, cost);
System.out.print("Enter count (0 to stop):");
count = conIn.nextInt();
}
}

}

我已经尝试了在这里找到的所有方法,以在输入完成后返回结果。我已经做了我的书上告诉我要做的事情,但我没有得到结果。只要朝着正确的方向轻推就会有所帮助。

最佳答案

您无法在任何地方打印该对象。打印对象

System.out.print(sb);

关于java - 无法弄清楚如何打印 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35618451/

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