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

huangapple go评论101阅读模式
英文:

I don'.t get the correct output in PocketTester

问题

以下是翻译好的部分:

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){
       	//使用if语句识别传入的字符串并为其提供对应的价值(以美分计)
       	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("请给出实际的硬币类型");
       	}
	}
  
	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;
   //你需要在这里添加更多的自定义类型实例变量
   public Pocket(){  //将初始值设为零
    totalValue = 0;
    currentValue = 0;
    }
   public void addCoin(String s, int i){
    // s表示硬币类型,你正在使用s实例化一个Coin对象并获取其价值
    // i表示硬币数量,你正在使用i将价值添加到totalValue
    if(s.equals("Dollar") || s.equals("Quarter") || s.equals("Dime") || s.equals("Nickel") || s.equals("Penny") && i == 0){
       System.out.println("请输入实际的硬币");
    }
    if(s.equals("Quarter") && i == 5){
       quarter = 125;
    }
    if(s.equals("Dime") && i == 3){
       dime = 30;
    }
    if(s.equals("Nickel") && i == 2){
       nickel = 10;
    }
    if(s.equals("Penny") && i == 7){
       penny = 7;
    }
    currentValue = quarter + dime + nickel + penny;
    }
   public int getValue(){
    return totalValue;
    }
   public void printTotal(){
        System.out.println(currentValue + " 美分");
        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();
	}
}
英文:

So this project that I started has three classes and each one does a specific thing. The Coin class that can be instantiated with the (String) chosen, Dollar, Quarter, Nickel, Dime, and Penny. Every coin obviously has a different value in cents and will return the value to the caller. My Pocket class instantiate serveral coin objects and use it as a custom object/type as instance fields. Then my last class called PocketTester creates an object called myPocket and will have an input of 5 quarters, 3 dimes, 2 nickels, and 7 cents and prints out the total value.

Output: 172 cents

Here are my classes but for some reason when i run main() it gives me 132 cents instead of 172 cents and I don't know why. I pretty sure I called the variables correctly. Could someone help fix this.

Btw you might ask me why I didn't do this in one class and imported Scanner and used that, I just felt like doing it this way.

And the first time I posted this someone decided to just flag this as a duplicate or repost when this was never a DUPLICATE OR REPORT SO DON'T.

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();
}
}

答案1

得分: 0

如果你的目标是编写仅在特定测试用例中正常工作的代码,那么你目前的代码应该可以正常运行。

之所以出现132而不是172,是因为你只计算了5枚25美分硬币(125)和7枚1美分硬币(7)。125 + 7 = 132。

为什么呢?因为在你的addCoin()方法中,你正在检查字符串匹配。如果字符串匹配"Nickels",则添加5美分硬币,但你的测试代码传入的是"Nickel",没有"s",所以没有被添加。1美分硬币也是同样情况。

尽管如此,我强烈建议你进一步学习如何使用变量,因为你使用了完全不必要的多个变量。你还通过检查传递给addCoin()的整数是否与预设值匹配,将代码限制在仅适用于你使用的确切测试用例上;如果你只是使用i并乘以添加的硬币的面值(25表示25美分,等等),你可以使这段代码在无论添加多少硬币的情况下都能正常工作。

我还建议在检查字符串匹配时使用equals()而不是==,你在Coin类中是这样做的,但在Pocket类中却没有;你之所以看到任何成功,是因为你的所有字符串都是字面值。

英文:

If your goal is to write code that only works properly in the exact, specific test case you are using, then what you have should work fine.

The reason you're getting 132 instead of 172 is because you are only counting the 5 quarters (125) and the 7 pennies (7). 125 + 7 = 132.

Why? Because in your addCoin() method you are checking for string matches. You add the nickels if the string matches "Nickels", but your test code is passing in "Nickel", without the s, so it's not being added. Same case with pennies.

All this being said, I would highly recommend you look into how to use variables some more, as you're using several variables that are entirely unnecessary. You're also limiting your code to only the exact test case you're using by checking that the integer you pass to addCoin() matches your pre-set values; if you simply use i and multiply by the value of whatever coin is added (25 for quarter, etc.) you can have this code work no matter what amount of coins you add.

I would also recommend using equals() instead of == when checking for string matches, which you're doing in the Coin class but not in the Pocket class; you're only seeing any success here because all of your Strings are literals.

huangapple
  • 本文由 发表于 2020年10月28日 07:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64564578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定