java - formatting chart to display temperate and precipitation levels -
java - formatting chart to display temperate and precipitation levels -
import java.util.scanner; import java.util.arrays; class annualclimate { public static void main (string [ ] args) { //declare , 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 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 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 f string preciplabel = "(inches)"; //initialize //input - inquire user temp , preciptation scale selection system.out.print("choose temperature scale (f = fahrenheit, c = celsius): "); string tempchoice = in.next(); system.out.print("choose precipitation scale (i = inches, c = centimeteres): "); string precipchoice = in.next(); if(tempchoice.equalsignorecase("c")) { for( int index = 0; index < temperature.length; index++) { //code assigning new c values temperature array temperature[index] = temperature[index] - 32 * 5/9; templabel="(c)"; } } //convert in values cm; replace 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 format , align info 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 code. basically, result should chart displays 3 columns: 12 months, temperature in degrees relative each month, , precipitation levels relative each month. lastly 2 need able alter depending on if user inputs celsius or fahrenheit , if user inputs inches or centimeters.
just side note, think concern have output. think doing horrendously wrong printf function, can't seem figure out!
p.s. started taking ap computer science in high school. it's extremely hard, right it's 3 , started thing @ 9 pm. no joke. really need help on this. give thanks you.
you have print values in loop, because array:
for (int =0;i<month.length;i++) { system.out.printf("%5s %18s %19s\n ", month[i], temperature[i], precipitation[i]); } java arrays printf
Comments
Post a Comment