• 12-02-2008, 01:54 PM
soc86
Can any one help with my if statement, i am trying to say if the value comm is less than or equal to 500 then comm will be 500.(I believe I have achieved that.

But want the value of comm not to change if it is over 501 and print out the last two lines, for some reason it is not doing that, any advise on what i'am doing wrong?

public class Currency{

public static void main (String[]arg){

double commRate;
String currency;
double exchange;
double amount;
double pence;
double comm=500;
double commF;

double pounds;
int payout;
double commPound;
double commPence;

int payoutPd;
int payoutPc;

System.out.println("Please enter the commission rate in percent");

System.out.println("Please enter one of the following currencies you would like to exchange");
System.out.println("Euros=EUR");
System.out.println("Japanese Yen=JPY");
System.out.println("US Dollars=USD");

System.out.println("Please enter the rate of exchange to Pence");

System.out.println("Please enter the amount of foreign currency to exchange");

pence=(int)(amount/exchange);

comm=pence*commRate/100;

commF=comm;

pounds=(pence/100);
pounds=pounds-(int)pounds;

pence=pence*100;

if(pence <=499){

System.out.println(amount+" "+currency+" goes directly to "+(int)pounds+" pounds "+pence+" pence");
System.out.println("Amount too small.");
}

else if (comm <=500){

comm=500;

commPound=comm/100;

commPence=commF/100-(int)commPound;
commPence=commPence*100;

payoutPd=(int)(pounds-commPound);
payoutPc=(int)(pence-commPence);

System.out.println(amount+" "+currency+" goes directly to "+(int)pounds+" pounds "+(int)pence+" pence commission is "+(int)commPound+" pounds "+(int)commPence+" pence.");
System.out.println("Actual sterling amounts is "+payoutPd+" pounds "+payoutPc+" pence");

}
}
}
• 12-02-2008, 01:56 PM
soc86
These are the three answers i should obtain if the inputs are as follows:

Input 1:
Commission rate (percentage): 1.5
Currency to convert from: JPY
Rate of exchange to Sterling pence: 1.73464
Amount to exchange: 327.55
Output 1:
327.55 JPY goes directly to 1 pounds 88 pence
Amount too small.

Input 2:
Commission rate (percentage): 2
Currency to convert from: CNY
Rate of exchange to Sterling pence: 0.1377
Amount to exchange: 327.55
Output 2:
327.55 CNY goes directly to 23 pounds 78 pence Commission is 5 pounds 0 pence
Actual sterling amount is 18 pounds 78 pence

Input 3:
Commission rate (percentage): 2.5
Currency to convert from: CNY
Rate of exchange to Sterling pence: 0.1377
Amount to exchange: 11428.15
Output 3:
11428.15 CNY goes directly to 829 pounds 93 pence Commission is 20 pounds 74 pence.
Actual sterling amount is 809 pounds 18 pence
• 12-02-2008, 02:10 PM
CJSLMAN
huh?
Code:

`pence=(int)(amount/exchange);`
Question:
• Why are you casting a double to an int for the pence variable? amount, exchange & pence are defined as doubles.

CJSL
• 12-02-2008, 02:30 PM
soc86
Because for my assignment I have been asked to have amount and exchange as doubles, also after my first statement (<=500) nothing works?
• 12-02-2008, 02:41 PM
Nicholas Jordan
Same question, keep everything as doubles right up until they just have to be an Int, if they in fact have to..here pence=(int)(amount/exchange); you cast pence to an int, then you do if <= 499 followed by <= 500 which will give few positive tests.

Do
Code:

`Double fiveHundred = new Double(500.00);`
Then wherever you need that value, do:
Code:

```if ( sampleValue.doubleValue() <= fiveHundred.doubleValue() ) {an action}else{other action}```
Just do all arithemtic on Doubles, going to an int only when and if needful. Use Integer.toString( resultValue.intValue()) if ints are actually needed.

Tediously work through the logic, that's the only way to get it working. There is no boilerplate for thinking through loop logic.
• 12-02-2008, 03:56 PM
CJSLMAN