原來的代碼是:
while(dollarsValue > 0)
{
while(dollarsValue >= 100 && testCash.registerHundredD > 0)
{
dollarsValue -= 100;
testCash.registerHundredD --;
changePayment.registerHundredD ++;
}
while(dollarsValue >= 50 && testCash.registerFiftyD > 0)
{
dollarsValue -= 50;
testCash.registerFiftyD --;
changePayment.registerFiftyD ++;
}
while(dollarsValue >= 20 && testCash.registerTwentyD > 0)
{
dollarsValue -= 20;
testCash.registerTwentyD --;
changePayment.registerTwentyD ++;
}
while(dollarsValue >= 10 && testCash.registerTenD > 0)
{
dollarsValue -= 10;
testCash.registerTenD --;
changePayment.registerTenD ++;
}
while(dollarsValue >= 5 && testCash.registerFiveD > 0)
{
dollarsValue -= 5;
testCash.registerFiveD --;
changePayment.registerFiveD ++;
}
while(dollarsValue >= 2 && testCash.registerTwoD > 0)
{
dollarsValue -= 2;
testCash.registerTwoD --;
changePayment.registerTwoD ++;
}
while(dollarsValue >= 1)
{
if(testCash.registerOneD > 0)
{
dollarsValue -= 1;
testCash.registerOneD --;
changePayment.registerOneD ++;
}
else
{
centsValue +=100;
dollarsValue -= 1;
}
}
}
while(centsValue > 0)
{
while(centsValue > 50 && testCash.registerFiftyC > 0)
{
centsValue -= 50;
testCash.registerFiftyC --;
changePayment.registerFiftyC ++;
}
while(centsValue >= 20 && testCash.registerTwentyC > 0)
{
centsValue -= 20;
testCash.registerTwentyC --;
changePayment.registerTwentyC ++;
}
while(centsValue >= 10 && testCash.registerTenC > 0)
{
centsValue -= 10;
testCash.registerTenC --;
changePayment.registerTenC ++;
}
while(centsValue >= 5)
{
if(testCash.registerFiveC > 0)
{
centsValue -= 5;
testCash.registerFiveC --;
changePayment.registerFiveC ++;
}
else
{
checkCash = false;
System.err.println("ERROR! System does not have enough cash for changes.");
}
}
}
我的改寫是:
if(dollarsValue > 0)
{
if(dollarsValue >= 100 && testCash.registerHundredD > 0)
{
for(int testRegHundredD = testCash.registerHundredD; testRegHundredD > 0; testRegHundredD--)
{
dollarsValue = dollarsValue - 100;
changePayment.registerHundredD++;
testCash.registerHundredD = testRegHundredD;
}
}
else if(dollarsValue >= 50 && testCash.registerFiftyD > 0)
{
for(int testRegFiftyD = testCash.registerFiftyD; testRegFiftyD > 0; testRegFiftyD--)
{
dollarsValue = dollarsValue - 50;
changePayment.registerFiftyD++;
testCash.registerFiftyD = testRegFiftyD;
}
}
else if(dollarsValue >= 20 && testCash.registerTwentyD > 0)
{
for(int testRegTwentyD = testCash.registerTwentyD; testRegTwentyD > 0; testRegTwentyD--)
{
dollarsValue = dollarsValue - 20;
changePayment.registerTwentyD++;
testCash.registerTwentyD = testRegTwentyD;
}
}
else if(dollarsValue >= 10 && testCash.registerTenD > 0)
{
for(int testRegTenD = testCash.registerTenD; testRegTenD > 0; testRegTenD--)
{
dollarsValue = dollarsValue - 10;
changePayment.registerTenD++;
testCash.registerTenD = testRegTenD;
}
}
else if(dollarsValue >= 5 && testCash.registerFiveD > 0)
{
for(int testRegFiveD = testCash.registerFiveD; testRegFiveD > 0; testRegFiveD--)
{
dollarsValue = dollarsValue - 5;
changePayment.registerFiveD++;
testCash.registerFiveD = testRegFiveD;
}
}
else if(dollarsValue >= 2 && testCash.registerTwoD > 0)
{
for(int testRegTwoD = testCash.registerTwoD; testRegTwoD > 0; testRegTwoD--)
{
dollarsValue = dollarsValue - 2;
changePayment.registerTwoD++;
testCash.registerTwoD = testRegTwoD;
}
}
else if(dollarsValue >= 1)
{
if(testCash.registerOneD > 0)
{
for(int testRegOneD = testCash.registerOneD; testRegOneD > 0; testRegOneD--)
{
dollarsValue = dollarsValue - 1;
changePayment.registerOneD++;
testCash.registerOneD = testRegOneD;
}
}
else
{
centsValue = centsValue + 100;
dollarsValue = dollarsValue - 1;
}
}
}
else if(centsValue > 0)
{
if(centsValue >= 50 && testCash.registerFiftyC > 0)
{
for(int testRegFiftyC = testCash.registerFiftyC; testRegFiftyC > 0; testRegFiftyC--)
{
centsValue = centsValue - 50;
changePayment.registerFiftyC++;
testCash.registerFiftyC = testRegFiftyC;
}
}
else if(centsValue >= 20 && testCash.registerTwentyC > 0)
{
for(int testRegTwentyC = testCash.registerTwentyC; testRegTwentyC > 0; testRegTwentyC--)
{
centsValue = centsValue - 20;
changePayment.registerTwentyC++;
testCash.registerTwentyC = testRegTwentyC;
}
}
else if(centsValue >= 10 && testCash.registerTenC > 0)
{
for(int testRegTenC = testCash.registerTenC; testRegTenC > 0; testRegTenC--)
{
centsValue = centsValue - 10;
changePayment.registerTenC++;
testCash.registerTenC = testRegTenC;
}
}
else if(centsValue >= 5)
{
if(testCash.registerFiveC > 0)
{
for(int testRegFiveC = testCash.registerFiveC; testRegFiveC > 0; testRegFiveC--)
{
centsValue = centsValue - 10;
changePayment.registerFiveC++;
testCash.registerFiveC = testRegFiveC;
}
}
else
{
checkCash = false;
System.err.println("ERROR! System does not have enough cash for changes.");
}
}
}
while(dollarsValue > 0)
{
while(dollarsValue >= 100 && testCash.registerHundredD > 0)
{
dollarsValue -= 100;
testCash.registerHundredD --;
changePayment.registerHundredD ++;
}
while(dollarsValue >= 50 && testCash.registerFiftyD > 0)
{
dollarsValue -= 50;
testCash.registerFiftyD --;
changePayment.registerFiftyD ++;
}
while(dollarsValue >= 20 && testCash.registerTwentyD > 0)
{
dollarsValue -= 20;
testCash.registerTwentyD --;
changePayment.registerTwentyD ++;
}
while(dollarsValue >= 10 && testCash.registerTenD > 0)
{
dollarsValue -= 10;
testCash.registerTenD --;
changePayment.registerTenD ++;
}
while(dollarsValue >= 5 && testCash.registerFiveD > 0)
{
dollarsValue -= 5;
testCash.registerFiveD --;
changePayment.registerFiveD ++;
}
while(dollarsValue >= 2 && testCash.registerTwoD > 0)
{
dollarsValue -= 2;
testCash.registerTwoD --;
changePayment.registerTwoD ++;
}
while(dollarsValue >= 1)
{
if(testCash.registerOneD > 0)
{
dollarsValue -= 1;
testCash.registerOneD --;
changePayment.registerOneD ++;
}
else
{
centsValue +=100;
dollarsValue -= 1;
}
}
}
while(centsValue > 0)
{
while(centsValue > 50 && testCash.registerFiftyC > 0)
{
centsValue -= 50;
testCash.registerFiftyC --;
changePayment.registerFiftyC ++;
}
while(centsValue >= 20 && testCash.registerTwentyC > 0)
{
centsValue -= 20;
testCash.registerTwentyC --;
changePayment.registerTwentyC ++;
}
while(centsValue >= 10 && testCash.registerTenC > 0)
{
centsValue -= 10;
testCash.registerTenC --;
changePayment.registerTenC ++;
}
while(centsValue >= 5)
{
if(testCash.registerFiveC > 0)
{
centsValue -= 5;
testCash.registerFiveC --;
changePayment.registerFiveC ++;
}
else
{
checkCash = false;
System.err.println("ERROR! System does not have enough cash for changes.");
}
}
}
我的改寫是:
if(dollarsValue > 0)
{
if(dollarsValue >= 100 && testCash.registerHundredD > 0)
{
for(int testRegHundredD = testCash.registerHundredD; testRegHundredD > 0; testRegHundredD--)
{
dollarsValue = dollarsValue - 100;
changePayment.registerHundredD++;
testCash.registerHundredD = testRegHundredD;
}
}
else if(dollarsValue >= 50 && testCash.registerFiftyD > 0)
{
for(int testRegFiftyD = testCash.registerFiftyD; testRegFiftyD > 0; testRegFiftyD--)
{
dollarsValue = dollarsValue - 50;
changePayment.registerFiftyD++;
testCash.registerFiftyD = testRegFiftyD;
}
}
else if(dollarsValue >= 20 && testCash.registerTwentyD > 0)
{
for(int testRegTwentyD = testCash.registerTwentyD; testRegTwentyD > 0; testRegTwentyD--)
{
dollarsValue = dollarsValue - 20;
changePayment.registerTwentyD++;
testCash.registerTwentyD = testRegTwentyD;
}
}
else if(dollarsValue >= 10 && testCash.registerTenD > 0)
{
for(int testRegTenD = testCash.registerTenD; testRegTenD > 0; testRegTenD--)
{
dollarsValue = dollarsValue - 10;
changePayment.registerTenD++;
testCash.registerTenD = testRegTenD;
}
}
else if(dollarsValue >= 5 && testCash.registerFiveD > 0)
{
for(int testRegFiveD = testCash.registerFiveD; testRegFiveD > 0; testRegFiveD--)
{
dollarsValue = dollarsValue - 5;
changePayment.registerFiveD++;
testCash.registerFiveD = testRegFiveD;
}
}
else if(dollarsValue >= 2 && testCash.registerTwoD > 0)
{
for(int testRegTwoD = testCash.registerTwoD; testRegTwoD > 0; testRegTwoD--)
{
dollarsValue = dollarsValue - 2;
changePayment.registerTwoD++;
testCash.registerTwoD = testRegTwoD;
}
}
else if(dollarsValue >= 1)
{
if(testCash.registerOneD > 0)
{
for(int testRegOneD = testCash.registerOneD; testRegOneD > 0; testRegOneD--)
{
dollarsValue = dollarsValue - 1;
changePayment.registerOneD++;
testCash.registerOneD = testRegOneD;
}
}
else
{
centsValue = centsValue + 100;
dollarsValue = dollarsValue - 1;
}
}
}
else if(centsValue > 0)
{
if(centsValue >= 50 && testCash.registerFiftyC > 0)
{
for(int testRegFiftyC = testCash.registerFiftyC; testRegFiftyC > 0; testRegFiftyC--)
{
centsValue = centsValue - 50;
changePayment.registerFiftyC++;
testCash.registerFiftyC = testRegFiftyC;
}
}
else if(centsValue >= 20 && testCash.registerTwentyC > 0)
{
for(int testRegTwentyC = testCash.registerTwentyC; testRegTwentyC > 0; testRegTwentyC--)
{
centsValue = centsValue - 20;
changePayment.registerTwentyC++;
testCash.registerTwentyC = testRegTwentyC;
}
}
else if(centsValue >= 10 && testCash.registerTenC > 0)
{
for(int testRegTenC = testCash.registerTenC; testRegTenC > 0; testRegTenC--)
{
centsValue = centsValue - 10;
changePayment.registerTenC++;
testCash.registerTenC = testRegTenC;
}
}
else if(centsValue >= 5)
{
if(testCash.registerFiveC > 0)
{
for(int testRegFiveC = testCash.registerFiveC; testRegFiveC > 0; testRegFiveC--)
{
centsValue = centsValue - 10;
changePayment.registerFiveC++;
testCash.registerFiveC = testRegFiveC;
}
}
else
{
checkCash = false;
System.err.println("ERROR! System does not have enough cash for changes.");
}
}
}
Loop循環,例如for, while不就是loop嗎?我在國外學java的,for跟while的叫法就是for loop跟while loop
说的这么别扭。
testCash.registerHundredD --; changePayment.registerHundredD ++; }
对应的for loop
if(testCash.registerHundredD > 0)
for(int testRegTenC = dollarsValue; testRegTenC > 0; testRegTenC-=100)