【发布时间】:2020-10-27 23:55:30
【问题描述】:
所以我开始的这个项目有三个课程,每个课程都做特定的事情。 Coin 类可以用所选的 (String)、Dollar、Quarter、Nickel、Dime 和 Penny 实例化。每个硬币显然都有不同的美分价值,并将价值返回给调用者。我的 Pocket 类实例化多个硬币对象并将其用作自定义对象/类型作为实例字段。然后我的最后一个名为 PocketTester 的类创建了一个名为 myPocket 的对象,并将输入 5 个硬币、3 个硬币、2 个镍币和 7 美分,并打印出总值。
输出:172 美分
这是我的课程,但由于某种原因,当我运行 main() 时,它给了我 132 美分而不是 172 美分,我不知道为什么。我很确定我正确地调用了变量。有人可以帮忙解决这个问题吗?
顺便说一句,你可能会问我为什么不在一个班级这样做,而是导入 Scanner 并使用它,我只是想这样做。
当我第一次发布此内容时,有人决定将其标记为重复或重新发布,而这从来都不是重复或报告,所以不要这样做。
public class Coin
{
private int value;
public int dollar;
public int quarter;
public int dime;
public int nickel;
public int penny;
public Coin(String s){
//Use if statement to identify incoming string and provide value in cents
if(s.equals("Dollar")){
dollar = 1;
}
else if (s.equals("Quarter")){
quarter = 25;
}
else if (s.equals("Dime")){
dime = 10;
}
else if (s.equals("Nickel")){
nickel = 5;
}
else if(s.equals("Penny")){
penny = 1;
}
else{
System.out.println("Give me an actual coin");
}
}
public int getValue()
{
return value;
}
}
public class Pocket
{
private int currentValue;
private int totalValue;
public int dollar;
public int quarter;
public int dime;
public int nickel;
public int penny;
//You need to add more custom type instance variables here
public Pocket(){ //Set initial value to zero
totalValue = 0;
currentValue = 0;
}
public void addCoin(String s, int i){
// s is type of coin, you are using s to instantiate a Coin and get value
// i is number of coins, you are using i to keep adding value to the totalValue
if(s == "Dollar" || s == "Quarter" || s == "Dime" || s == "Nickel" || s == "Penny" && i == 0){
System.out.println(" Input an actual Coin ");
}
if(s == "Quarter" && i == 5){
quarter = 125;
}
if(s == "Dimes" && i == 3){
dime = 30;
}
if(s == "Nickels" && i == 2){
nickel = 10;
}
if(s == "Penny" && i == 7){
penny = 7;
}
currentValue = quarter + dime + nickel + penny;
}
public int getValue(){
return totalValue;
}
public void printTotal(){
System.out.println(currentValue+ " cents");
System.out.println();
}
}
public class PocketTester
{
public static void main(String args[])
{
Pocket myPocket = new Pocket();
myPocket.addCoin("Quarter", 5);
myPocket.addCoin("Dime", 3);
myPocket.addCoin("Nickel", 2);
myPocket.addCoin("Penny", 7);
myPocket.printTotal();
}
}
【问题讨论】:
-
不要使用
==运算符 (s == "Dollar") 进行字符串比较是否相等。这是错误的。请改用 String#equals() 方法或 String#equalsIgnoreCase() 方法(在这种情况下后者更好)。s.equalsIgnoreCase("dollar") -
由于您在
cents工作,美元应该等于100(不是1)。addCoin()方法应该像这样计算每个货币面额:if (s.equalsIgnoreCase("quarter")) { quarter = 25 * i; }。最好为此使用switch/case,而不是一堆if/else if语句。 -
为此:
if (s == "Dollar" || s == "Quarter" || s == "Dime" || s == "Nickel" || s == "Penny" && i == 0) { System.out.println(" Input an actual Coin "); }真正有效应该是这样的:if ((s.equalsIgnoreCase("Dollar") || s.equalsIgnoreCase("Quarter") || s.equalsIgnoreCase("Dime") || s.equalsIgnoreCase("Nickel") || s.equalsIgnoreCase("Penny")) && i == 0) { System.out.println("Input an actual number of coins for: " + s); return; }。请注意所有||条件是如何包含在一组括号中的?
标签: java object if-statement instantiation custom-object