java - 更改数组值后打印数组

标签 java arrays

我正在尝试将数组温度转换为摄氏度,但它们一直输出为 16.0,我怎样才能转换所有数组值并单独打印它们?都是按照月份排序的,需要将数组值转换后按照月份打印。

以下是我的代码:

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
}

最佳答案

您仅打印变量摄氏度。但您真正需要打印的是相关的温度值。因此,创建一个数组并在计算时放入温度值并打印它们。只需创建一个数组即可。

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

` 并在计算时存储值

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


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

           celsius_temp[index]=celsius;
           index++;

        }

    }

并打印表格

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++;


    }

关于java - 更改数组值后打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086877/

相关文章:

java - Java Spring Boot 的 Docker 基础镜像 (`FROM` 是什么?

java - 在 liberty 中动态配置 dev 和 prod 数据源以根据环境加载

java - 在 Tomcat 中作为 WAR 运行的 Grails 项目中 Maven-build JAR 的 SLF4J 配置位置

javascript - 如何比较和过滤es6中两个对象数组中不匹配的数组

python - 迭代搜索循环列表

python - 将一个 numpy 数组取另一个数组的子集

java - 在 Java 中重置 FileInputStream 以便我可以在测试程序中运行另一个程序的多个实例

java - 创建 bean entityManagerFactory 时出错,NoSuchMethodError : javax/persistence/Table. 索引

整数键上的 PHP array_merge_recursive 行为

c - 如何从函数返回 float 组?