打印不同长度的阵列

时间:2013-12-17 01:08:42

标签: java arrays printing double

我正在编写一个打印一个数组的程序,该数组包含作为参数传递的两个数组的值之和。当其中一个数组可能比另一个数组长时,我需要包含异常。在这种情况下,该方法应该打印两个数组共享的索引的总和,然后打印没有要添加的相应值的数组的值。

这里的问题是,每次我编写程序时,都会收到一条消息: Sum.java:51:找不到符号 符号:变量sumY location:class Sum                     sumY [j] = x [j] + y [j];                     ^ 1错误

import java.util.*;

class Sum
{
    public static void main (String [] args)
    {
        double [] a1 = {4.5, 2.8, 3.4, 0.8};
        double [] a2 = {1.4, 8.9, -1.0, 2.3};

        arraySum (a1, a2);

        System.out.println (Arrays.toString (arraySum (a1, a2)));   
    }

    public static double [] arraySum (double [] x, double [] y)
    {
        int length = 0;

        if (x.length < y.length)
        {
            double [] sumY = new double [y.length];
            length = y.length;

            for (int i = 0; i <= y.length - 1; i++)
            {
                for (int j=0; j <= x.length-1; j++)
                {
                    sumY [j] = x[j] + y[j];
                }

                sumY [(x.length -1) + i] = y[i];
            }

            return sumY;        
        }

        if (x.length > y.length)
        {
            double [] sumX = new double [x.length];
            length = x.length;

            for (int i = 0; i <= x.length - 1; i++)
            {
                for (int j=0; j <= y.length-1; j++)
                {
                    sumY[j] = x[j] + y[j];
                }

                sumX [(y.length -1) + i] = y[i];
            }

            return sumX;
        }

        else
        {
            double [] sum = new double [x.length];
            length = x.length;

            for (int i = 0; i <= length - 1; i++)
            {
                sum[i] = x[i] + y[i];
            }

            return sum;
        }

    }
}

3 个答案:

答案 0 :(得分:1)

1:使用

而不是使用Arrays.toString
System.out.printf("[");
double[] sum = arraySum(a1, a2);
for (int i = 0; i < sum.length; i++) {
    System.out.printf("%0.1f", sum[i]);
    if(i != sum.length - 1) {
        System.out.printf(", ");
    }
}
System.out.printf("]\n");

2:在第二个if块中,你使用sumY我收集你的意思是sumX。 sumY没有在该范围内定义,所以它给你一个错误。

答案 1 :(得分:1)

您还可以通过实施方法缩短代码。在这种情况下,我称之为altMethod。此外,您应该注意到altMethod不会在for循环中使用for循环,而是使用另一个循环。第一个填写总和,而索引可以配对,下一个只填写剩下的索引。

import java.util.Arrays;

public class MySum {

    public static void main (String [] args) {
        double [] a1 = {4.5, 2.8, 3.4, 0.8};
        double [] a2 = {1.4, 8.9, -1.0, 2.4};

        System.out.println (Arrays.toString (arraySum(a1, a2)));   
    }

    public static double [] arraySum (double [] x, double [] y) {

        if (x.length < y.length) {
            return altMethod(x, y);
        }

        if (x.length > y.length) {
            return altMethod(y, x);
        }

        else {
            return altMethod(x, y);
        }

    }

    public static double[] altMethod(double[] smaller, double[] bigger) {
        int length = 0;
        double[] answer = new double[bigger.length];

        for(int i = 0; i < smaller.length ; i++) {
            answer[i] = smaller[i] + bigger[i];
            length++;
        }

        for(;length < bigger.length; length++) {
            answer[length] = bigger[length];
        }

        return answer;
    }
}

altMethod确实必须以某种顺序接受参数。 (smaller, bigger) (bigger, smaller)而不是<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.firebase.quickstart.fcm"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <!-- [START fcm_default_icon] --> <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. --> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_stat_ic_notification" /> <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. --> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" /> <!-- [END fcm_default_icon] --> <activity android:name="com.google.firebase.quickstart.fcm.MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- [START firebase_service] --> <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <!-- [END firebase_service] --> <!-- [START firebase_iid_service] --> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> <!-- [END firebase_iid_service] --> <service android:name=".MyJobService" android:exported="false"> <intent-filter> <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/> </intent-filter> </service> </application>

答案 2 :(得分:0)

这是因为最接近的double到2.3和最接近的double到0.8,当加在一起时,不会将最接近的double提供给3.1。

如果您要使用精确小数进行算术运算并期望得到正确答案,则应使用BigDecimal类而不是double