i just want to write a code for simple fare calculator using conditional statements in JAVA.

whose out should be:
Enter the distance covered: 22.6

First 5 KM  : 50 Rs.
Next 15 KM @12 Rs: 180 Rs.
Next 2.6 KM @14 Rs. :36.4 Rs.

Recommended Answers

All 3 Replies

Then fire up your text editor and start writing!
When you get stuck then come back here, show what you have done, and explain what’s stopping you.

@JamesCherrill

I tried it many times and done it my self

import java.util.Scanner;

class FareCalculator{
public static void main(String arr[]){
    Scanner s = new Scanner(System.in);
    System.out.print("enter distance covered: ");
    int a = s.nextInt();
    int x=0,y=0;
    if(a<=5){
        System.out.println("fare for first 5KM : 50Rs");
        System.out.println("total distance: " +a+ " fare:50" );
    }   
    else if(a>5&&a<=20)
        {
        System.out.println("first 5 km fare : 50 Rs.");
        x = a-5;
        y=x*12;
        System.out.println("next " +x+ " KM fare @12 : " +y+ " Rs.");
        y=y+50;
        System.out.println("Total fare will be  : " +y+ " Rs.");
    }
    else if(a>20){
        System.out.println("first 5 km fare : 50 Rs.");
        x=20;
        y=20*12;
        System.out.println("next " +x+ " KM fare @12 : " +y+ " Rs.");
        int z = a-20;
        int b = z*14;
        System.out.println("next " +z+ " km  fare @14: " +b+ " Rs.");
        int c=y+b+50;
        System.out.println("Total fare will be  : " +c+ " Rs.");
    }
}   
}

It looks to me your first most obvious problem is, this code can't run with the example you've given. Your example is a float value but all your variables are int, including your call to the scanner object.

Once you have code that will run, now you can look at your output and see where your calaculations are wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.