import java.util.Scanner;
import java.util.Arrays;
class AnnualClimate
{
public static void main (String [ ] args)
{
//Declare and intialize variables
Scanner in = new Scanner(System.in);
String city = "West Palm Beach";
String state = "Florida";
String month [] ={" Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ", " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec "};
double temperature [] = {66.2, 67.2, 70.6, 73.8, 78.2, 81.2, 82.5, 82.8, 81.7, 78.1, 73.1, 68.3}; //initialize with Fahrenheit values
double precipitation [] = {3.8, 2.6, 3.7, 3.6, 5.4, 7.6, 6.0, 6.7, 8.1, 5.5, 5.6, 3.1}; //initialize with inches values
double total[][]={{66.2, 67.2, 70.6, 73.8, 78.2, 81.2, 82.5, 82.8, 81.7, 78.1, 73.1, 68.3},{3.8, 2.6, 3.7, 3.6, 5.4, 7.6, 6.0, 6.7, 8.1, 5.5, 5.6, 3.1}};
String tempLabel = "(F)"; //initialize to F
String precipLabel = "(inches)"; //initialize to i
//INPUT - ask user for temp and preciptation scale choice
System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
String tempChoice = in.next();
System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): ");
String precipChoice = in.next();
if(tempChoice.equalsIgnoreCase("C"))
{
for( int index = 0; index < temperature.length; index++)
{
//code for assigning new C values to the temperature array
temperature[index] = temperature[index] - 32 * 5/9;
tempLabel="(C)";
}
}
//Convert in values to cm; replace the current values in precipitation
if(precipChoice.equalsIgnoreCase("c"))
{
for( int index = 0; index < precipitation.length; index++)
{
precipitation[index] = precipitation[index] * 2.54;
precipLabel="(cm)";
}
}
//OUTPUT - print table using printf to format and align data
System.out.println();
System.out.println("Climate Data");
System.out.println("Location: " + city +", " + state);
System.out.printf("%5s %18s %s %18s %s","Month", "Temperature" ,tempLabel,"Precipitation", precipLabel);
System.out.println();
System.out.printf("***************************************************");
System.out.println();
System.out.printf("%5s %18s %19s ", month, temperature, precipitation);
}
}
This is my code. Basically, the result should be a chart that displays three columns: The 12 months, the temperature in degrees relative to each month, and the precipitation levels relative to each month. The last two need to be able to change depending on if the user inputs Celsius or Fahrenheit and if the user inputs inches or centimeters.
Just another side note, I think the only concern i have is the output. I think I am doing something horrendously wrong with the printf function, but I can't seem to figure it out!
P.S. just started taking AP Computer Science in high school. It's extremely hard, right now it's 3 AM and I started this thing at 9 PM. No joke. So I really really really need help on this. Thank you.