I'm doing a project with command line and array. A program that converts F -> C and C -> F. Here what I got so far:
public class Implementation
{
public static void main(String[] args)
{
double degree = 0;
String celsius = null;
String fahrenheit;
int n = 0;
String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};
if (degree < 0)
{
n = 0;
}
if (degree >= 0 && degree < 32)
{
n = 1;
}
if (degree >= 32 && degree < 50)
{
n = 2;
}
if (degree >= 50 && degree < 60)
{
n = 3;
}
if (degree >= 60 && degree < 70)
{
n = 4;
}
if (degree >= 70 && degree < 90)
{
n = 5;
}
if (degree >= 90)
{
n = 6;
}
if (args.length != 3)
{
System.out.println("Error! Please try again.");
System.exit(0);
}
else
{
degree = Double.parseDouble(args[0]);
celsius = args[1];
fahrenheit = args[2];
}
switch (celsius)
{
case "c":
System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
break;
case "f":
System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
break;
}
}
public static double celsius(double fahrenheitTemperature)
{
return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
}
public static double fahrenheit(double celsiusTemperature)
{
return ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
}
}
I have problem in Array part. I couldn't get the right array days[] with the specific degree. For example, If I do command line: 100 c f, it supposes to show up 100 Celsius is 212.0 Fahrenheit Cold "Hot". But my program only shows up "Cold", no matter what degree I input.