0

Task: Write a program that calculates the mileage and/or cost of fuel by asking the user to input two cities.

It only works for two places and not all three, why?

How can I put just the name of the location instead of inputting the numbers?

```
package Winterproject;
import java.lang.Math;
import java.lang.String;
import java.util.Scanner;
public class Mileage {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String LocationNames;
int Location1=0;
int Location2=0;
int Location3=0;
double Distance1;
double lat1 = 0;
double lat2 = 0;
double Distance2;
double lng1 = 0;
double lng2 = 0;
double distance1=0;
double c = 0;
double distance2=0;
double Radius = 3961;
double costoffuel = 3.64;
System.out.println("Write your starting location.");
Location1=in.nextInt();
// System.out.println("Choose a city: enter 1 for Newyork, 2 for California, and 3 for kansas.");
//City2=in.nextInt();
System.out.println("Write your destination.");
Location3=in.nextInt();
switch(Location1){
case 1:
lat1 =33;
lng1 =84;
LocationNames="Atlanta";
break;
case 2:
lat2=27;
lng2= 122;
LocationNames="Seattle";
break;
case 3:
lat2=21;
lng2= 157;
LocationNames="Honolulu";
break;
default:
}
switch(Location2){
case 1:
lat1 =33;
lng1 =84;
LocationNames="Atlanta";
break;
case 2:
lat2=27;
lng2= 122;
LocationNames="Seattle";
break;
case 3:
lat2=21;
lng2= 157;
LocationNames="Honolulu";
break;
default:
}
switch(Location3){
case 1:
lat1 =33;
lng1 =84;
LocationNames="Atlanta";
break;
case 2:
lat2=27;
lng2= 122;
LocationNames="Seattle";
break;
case 3:
lat2=21;
lng2= 157;
LocationNames="Honolulu";
break;
default:
System.out.println("Wrong input.");
}
Distance2 = (lat2 - lat1) *(lat2 -lat1) ;
Distance1 = (lng2 - lng1) * (lng2 -lng1);
distance1 =(Math.sin(Distance2/2)) +Math.cos(lat2) * Math.cos(lat1) * Math.sin(Distance1/2);
c = 2 * Math.atan2(Math.sqrt(distance1),Math.sqrt(1+distance1)) ;
distance2 = Radius * c/2.03833;
System.out.println("Distance between the two locations: " + distance2 );
//System.out.println("howmany");
double mileage = (distance2/costoffuel);
System.out.println("Mileage: "+mileage);
}
}
```