【问题标题】:I want to make the printer print until the balance in the account is not enough for the printing job我想让打印机打印,直到帐户中的余额不足以打印作业
【发布时间】:2012-12-03 15:50:50
【问题描述】:

问题是我的余额一直是负数,它执行了需要取出钱的部分,但我设置的“if”语句应该可以防止这种情况发生。我是编程新手,如果这很愚蠢,我很抱歉。

这是我的主要内容:

public class PrinterAccount
{
    private int balance;

    public void topUp(int amount)
    { 
        balance += amount;
    }
    public int getBalance()
    { 

        return balance;
    }
    public boolean printDocument(int numPages, boolean isDoubleSided)
    {

        if (isDoubleSided)
        {
            if(numPages % 2 == 0)
            {
                int b = 0;
                b = b - ((numPages/2)*5);
                    if (balance > b)
                    {
                        balance -= ((numPages/2) * 5);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }
            else
            {
                numPages = numPages + 1;
                int a = 0;
                a = a - ((numPages/2)*5);
                    if (balance > a)
                    {
                        balance -= ((numPages/2) * 5);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }
        }
        else
        {
            int c = 0;
            c = c - (numPages*5);
                if (balance > c)
                {
                    balance -= (numPages*5);
                    return true;
                }
                else
                {
                    return false;
                }

        }   
    }
}

这是我的测试:

public class PrinterAccountTest
{
    public static void main(String[] args)
    {
        // Create an object of class PrinterAccount
        PrinterAccount myprinterAccount = new PrinterAccount();
        // Add 10 pounds credit (1000 pence)
        myprinterAccount.topUp(1000);

        // **************TEST 1************************
        // Print a 15-page document, single-sided, should cost 75p
        boolean res = myprinterAccount.printDocument(15, false);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 925
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 2************************
        // Print a 20-page document (even number of pages), double-sided, should cost 50p
        res = myprinterAccount.printDocument(20, true);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 875
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 3************************
        // Print a 7-page document (odd number of pages), double-sided, should cost 20p
        res = myprinterAccount.printDocument(7, true);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 855
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 4************************
        // Print a 200-page document, single-sided, should cost 1000p
        res = myprinterAccount.printDocument(200, false);
        // Should get a message saying the document failed to print
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 855
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());


    }
}

这是我运行代码时得到的结果:

The document printed successfully
Remaining balance is 925
The document printed successfully
Remaining balance is 875
The document printed successfully
Remaining balance is 855
The document printed successfully
Remaining balance is -145

但我需要它显示:

The document printed successfully
Remaining balance is 925
The document printed successfully
Remaining balance is 875
The document printed successfully
Remaining balance is 855
The document failed to print: not enough credit.
Remaining balance is 855

【问题讨论】:

    标签: java variables methods instance private


    【解决方案1】:

    打印文档方法中的这行:

        int c = 0;
        c = c - (numPages*5);
        if (balance > c) ...
    

    变量 c 将始终具有负值。我想您想添加 (numPages*5) 而不是减去它。

    【讨论】:

      【解决方案2】:

      因为您的 balance 将始终大于 a、b、c,因为 c 将为负数,而余额将为正值 balance>c 将始终为真。换个逻辑试试

      【讨论】:

        猜你喜欢
        • 2018-09-27
        • 1970-01-01
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        • 2018-06-18
        • 1970-01-01
        相关资源
        最近更新 更多