我没有在 PocketTester 中得到正确的输出

I don'.t get the correct output in PocketTester

所以我开始的这个项目有三个 classes,每个都做一件特定的事情。可以使用所选的 (String)、Dollar、Quarter、Nickel、Dime 和 Penny 实例化的 Coin class。每个硬币显然都有不同的美分价值,并将 return 价值传递给调用者。我的口袋 class 实例化几个硬币对象并将其用作自定义 object/type 作为实例字段。然后我最后一个名为 PocketTester 的 class 创建了一个名为 myPocket 的对象,并将输入 5 个 25 美分、3 个角钱、2 个镍币和 7 美分,并打印出总价值。

输出:172 美分

这是我的 classes 但出于某种原因,当我 运行 main() 它给了我 132 美分而不是 172 美分,我不知道为什么。我很确定我正确地调用了变量。有人可以帮忙解决这个问题吗?

顺便说一句,你可能会问我为什么我不在一个 class 中这样做,而是导入 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();
    }
}




如果您的目标是编写仅在您使用的确切、特定测试用例中正常工作的代码,那么您所拥有的应该可以正常工作。

你得到 132 而不是 172 的原因是因为你只计算了 5 个季度 (125) 和 7 个便士 (7)。 125 + 7 = 132.

为什么?因为在您的 addCoin() 方法中您正在检查字符串匹配。如果字符串匹配“Nickels”,则添加镍,但您的测试代码传入“Nickel”,没有 s,因此不会添加。与便士相同。

综上所述,我强烈建议您多研究一下如何使用变量,因为您正在使用几个完全不必要的变量。您还通过检查传递给 addCoin() 的整数是否与您的 pre-set 值匹配,将您的代码限制为仅使用您正在使用的确切测试用例;如果您简单地使用 i 并乘以添加的任何硬币的价值(每季度 25,等等)无论您添加多少硬币,您都可以使此代码工作。

我还建议在检查字符串匹配时使用 equals() 而不是 ==,这是在 Coin class 而不是 Pocket class;你在这里只看到任何成功,因为你所有的字符串都是文字。