如何将0初始化为两个构造函数?

时间:2018-02-27 02:28:57

标签: java

h我将令牌计数的值分配给0,但似乎无法正常工作。我不知道我的代码有什么问题。我认为我的代码有效,但我不知道如何使输出中的令牌计数为0.我在NetBeans中运行代码,但输出为"状态为true 预期:是的, 令牌数:2, 预计:2, 可以数:18, 预计:8"我正在使用Java。

public class VendingMachine {
public int canCount;
public int tokenCount;
VendingMachine() {
    canCount = 10;
   tokenCount = 0;
}
VendingMachine(int cans) {
    canCount = cans;
   tokenCount = 0;
}
public void add(int cans) {
    canCount = canCount + cans;
}
public boolean insertToken() {
    if (canCount <= 0) {
        return false;
    } else {
        canCount = canCount - 1;
        tokenCount = tokenCount + 1;
    }
    return true;
}
public int getCanCount() {
    return canCount;
}
public int getTokenCount() {
    return tokenCount;
}

}

这是我的测试人员代码。

public class VendingMachineTester {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    VendingMachine machine = new VendingMachine();
    machine.add(10);
    boolean status;
    machine.insertToken();
    status = machine.insertToken();
    System.out.println("Status " + status);
    System.out.println("Expected: true");
    System.out.print("Token count: ");
    System.out.println(machine.getTokenCount());
    System.out.println("Expected: 2");
    System.out.print("Can count: ");
    System.out.println(machine.getCanCount());
    System.out.println("Expected: 8");
}

}

0 个答案:

没有答案