問題是我一直將餘額減去,它執行需要將錢取出的部分,但是我已經實施的'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
OMG,多麼愚蠢的錯誤......非常感謝。 – Waj