# Help with IF ELSE

• 09-21-2010, 01:44 AM
LiquidJava
Help with IF ELSE
I got this question to complete in college recently. I am new to Java so slowly learning, there could be some mistakes

Quote:

Calculations and if – else (the sponsored cyclist). A cyclist is sponsored at the rate of 7c for each km up to 10 km, and 10c for each km in excess of 10km. Write a program which will ask for the cyclist’s first name, initial and last name and the distance cycled. Your program should calculate the money due, then display a dialog containing the cylist’s full name (first name + initial + last name), the distance cycled, and the money due. Note here that the problem involves 2 different rates if the number of km exceeds 10 so, for example, if the cyclist is sponsored for 15km then the first 10km are sponsored at 7c and the remaining 5 km are sponsored at 10c giving a total of 70c + 50c i.e. €1.20. Your solution should work correctly no matter what the distance is.
This is what I have done so far
Code:

```import javax.swing.JOptionPane; public class Cyclist{         public static void main (String args []){                 String firstName,lastName,initialAsString;                 char initial;                 int distanceTravelled;                 firstName = JOptionPane.showInputDialog ("Please enter your name");                 initialAsString = JOptionPane.showInputDialog ("Please enter your initial");                 initial = initialAsString.charAt(0);                 lastName = JOptionPane.showInputDialog ("Please enter your last name");                 distanceTravelled = Integer.parseInt (JOptionPane.showInputDialog ("Please enter the distance travelled"));                 if (distanceTravelled < 10){                         JOptionPane.showMessageDialog(null, "Your name is: "+firstName+ " ."+initial +                                 " "+lastName+"\nDistance Travelled: "+distanceTravelled+"km\nThe amount " +                                         " of money due is " +distanceTravelled*7);                 }else{                 if (distanceTravelled > 10){                         JOptionPane.showMessageDialog(null, "Your name is: "+firstName+ " ."+initial +                                 " "+lastName+"\nDistance Travelled: "+distanceTravelled+"km\nThe amount " +                                         " of money due is " +distanceTravelled*10);                 }               }     } }```
I'm having a problem with this part
Quote:

if the cyclist is sponsored for 15km then the first 10km are sponsored at 7c and the remaining 5 km are sponsored at 10c giving a total of 70c + 50c i.e. €1.20. Your solution should work correctly no matter what the distance is.
I would appreciate some help. Thanks
• 09-21-2010, 01:59 AM
jlmp
Your problem is algorithmic...you can make different solutions for this:

I am editing this: Lets see (cause i am sleepless), think about this, when you take a number which is over 10 all you have to do is take the first 10 and multiply with 7 and the rest and multiply it with 10...so

if you have 25 you can do something like this 25-10=15!

so,
Code:

```if (distanceTravelled > 10){ // multiply 10*7 and put the value in a temp int // distanceTravelled=distanceTravelled-10; // then mutiply the distanceTravelled with 10 and then add this with temp }```
This maybe a solution for your problem but i am saying that u can solve it with different ways!
• 09-21-2010, 02:13 AM
javaman1
I agree with Jlmp however please enclose your code in [ code] and [/ code], without the spaces, next time.
• 09-21-2010, 02:30 AM
Eranga
Quote:

Originally Posted by LiquidJava
I'm having a problem with this part I would appreciate some help. Thanks

What you've tried so far, can you show it here?
• 09-29-2010, 04:32 PM
LiquidJava
The only code I have done so far is the code I have posted.