简体   繁体   中英

Printing out arrays after changing their value

I'm trying to convert the array temperature into celsius but they keep outputting as 16.0, how can I able to convert all of the array values and print them separately? They are all in order according to the month, the array values need to be converted and then printed according to the month.

The following is my code:

import java.util.Scanner;

class AnnualClimate
{
    public static void main( String[] args )
    {
        // Declare and intialize variables - programmer to provide initial values
        Scanner in = new Scanner( System.in );
        String city = "Daytona Beach";
        String state = "Florida";
        int o = 0;
        double celsius = 0;
        int index = 0;

        String month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        double temperature[] = { 58.4, 60.0, 64.7, 68.9, 74.8, 79.7, 81.7, 81.5, 79.9, 74.0, 67.0, 60.8 };
        double precipitation[] = { 3.1, 2.7, 3.8, 2.5, 3.3, 5.7, 5.2, 6.1, 6.6, 4.5, 3.0, 2.7 };
        String tempLabel = "F"; // initialize to F
        String precipLabel = "inch"; // initialize to inch

        // 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();

        // PROCESSING - convert from F to C and in to cm based on user's choices

        // remember 5/9 = 0, 5.0/9 = .5555

        if ( tempChoice.equalsIgnoreCase( "C" ) )
        {
            tempLabel = "(C)";
            for ( index = 0; index < temperature.length; )
            {
                celsius = ( temperature[ index ] - 32 ) * 5 / 9;
                index++;
            }
        }

        // Convert in values to cm; replace the current values in precipitation
        if ( precipChoice.equalsIgnoreCase( "c" ) )
        {
            precipLabel = "(cm)";
            for ( int i = 0; i < precipitation.length; i++ )
            {
                double centimeters = precipitation[ i ] * 2.54;
            }
        }

        // 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.printf( "%n" );
        System.out.printf( "***************************************************" );

        while ( o < month.length )
        {
            if ( tempChoice.equalsIgnoreCase( "C" ) )
            {
                System.out.printf( "%n" );
                System.out.printf( month[ o ] );
                System.out.printf( "%20.2f", celsius );
                System.out.printf( "%25.2f%n", precipitation[ o ] );
                o++;
            }
        }
        System.out.println();
        System.out.printf( "***************************************************" );
        System.out.println();
    }// end main
}

you are printing only the variable celsius . But what you really need to print is relevant temprature value. So create an array and put the temprature values when they are calculating and print them.Just create an Array.

 Double[] celsius_temp=new Double[temperature.length];

` And store values when calculating

if(tempChoice.equalsIgnoreCase("C"))
    {
        tempLabel="(C)";


        for( index = 0; index < temperature.length;)
        {
           celsius= (temperature[index]-32)*5/9;

           celsius_temp[index]=celsius;
           index++;

        }

    }

and print the table

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.printf("%n");
    System.out.printf("***************************************************");


    while ( o< month.length)
    {

     if(tempChoice.equalsIgnoreCase("C"))
    {
        System.out.printf("%n");
        System.out.printf(month[o]);
        System.out.printf("%20.2f", celsius_temp[o]);
        System.out.printf("%25.2f%n", precipitation[o]);
        o++;


    }
 for( index = 0; index < temperature.length;)
            {
               celsius= (temperature[index]-32)*5/9;
               index++;

            }

You're only storing one value and printing it out, it'll be the last value in the array because of the loop going through each one, but the next value overwrites the last. You should be converting each value and storing each of them into an array.

You can make celsius into an array of same size as temperature to hold each converted value.

You have a result array to store the converted value.

Here is modified version of your code segment:

double[] celsius = new double[ temperature.length ]; // added!!!
if ( tempChoice.equalsIgnoreCase( "C" ) )
{
    tempLabel = "(C)";
    for ( index = 0; index < temperature.length; )
    {
        celsius[ index ] = ( temperature[ index ] - 32 ) * 5 / 9; // modified!!!
        index++;
    }
}

// Convert in values to cm; replace the current values in precipitation
double[] centimeters = new double[ precipitation.length ]; // added!!!
if ( precipChoice.equalsIgnoreCase( "c" ) )
{
    precipLabel = "(cm)";
    for ( int i = 0; i < precipitation.length; i++ )
    {
        centimeters[ i ] = precipitation[ i ] * 2.54; // modified!!!
    }
}

And modify the output code segment:

while ( o < month.length )
{
    if( tempChoice.equalsIgnoreCase("C") )
    {
        System.out.printf("%n");
        System.out.printf(month[o]);
        System.out.printf("%20.2f", celsius[ o ] ); // modified
        System.out.printf("%25.2f%n", precipitation[ o ]); // modified
        o++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM