Java:打印数组元素

时间:2016-05-11 01:41:20

标签: java arrays printing

我需要以每行10行的增量打印数组元素......

import java.util.*;

public class Array {

    public static void main(String args[]) {
        double alpha[] = new double[50];

        //Initialize the first 25 elements of the array (int i=0; i<25; i++)//
        for(int i = 0; i < 25; i++) {
            alpha[i]= i * i;
        }

        //Initialize the last 25 elements of the array (i=25; i<50; i++)//
        for(int i = 25; i < 50; i++) {
            alpha[i]= 3 * i;
        }

        //Print the element of the array
        System.out.println ( "The values are: " );

        for (int i = 0; i < 50; i++) {
            System.out.println ( alpha[i] );
        }

        //Print method to display the element of the array
        void print(double m_array[]) {
            for(int i =1; i <= m_array.length; i++) {
                System.out.print(m_array[i-1] +" ");
                if(i%10==0)
                    System.out.print("\n");
            }
        }
    }
}

我假设这个,如果声明是我的问题...如果它正是我错过了我尝试了printf但是没有工作......

2 个答案:

答案 0 :(得分:1)

public class Array {

    public static void main(String args[]) {
        double alpha[] = new double[50];

        // Initialize the first 25 elements of the array (int i=0; i<25; i++)
        for (int i = 0; i < 25; i++) {
            alpha[i] = i * i;
        }

        // Initialize the last 25 elements of the array (i=25; i<50; i++)
        for (int i = 25; i < 50; i++) {
            alpha[i] = 3 * i;
        }

        System.out.println("The values are: ");

        // Print the element of the array
        print(alpha);

    }

    // Print method to display the element of the array
    private static void print(double m_array[]) {
        for (int i = 1; i <= m_array.length; i++) {
            System.out.print(m_array[i-1] + " ");
            if (i % 10 == 0) {
                System.out.println();
            }
        }
    }
}

输出:

  

值为:

     

0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0

     

100.0 121.0 144.0 169.0 196.0 225.0 256.0 289.0 324.0 361.0

     

400.0 441.0 484.0 529.0 576.0 75.0 78.0 81.0 84.0 87.0

     

90.0 93.0 96.0 99.0 102.0 105.0 108.0 111.0 114.0 117.0

     

120.0 123.0 126.0 129.0 132.0 135.0 138.0 141.0 144.0 147.0

答案 1 :(得分:0)

public class Array {

    public static void main(String args[]) {
        double alpha[] = new double[50];

        // Initialize the first 25 elements of the array (int i=0; i<25; i++)
        for (int i = 0; i < 25; i++) {
            alpha[i] = i * i;
        }

        // Initialize the last 25 elements of the array (i=25; i<50; i++)
        for (int i = 25; i < 50; i++) {
            alpha[i] = 3 * i;
        }

        System.out.println("The values are: ");

        // Print the element of the array
        print(alpha);

        double number = 4.0;
        if (hasElement(alpha, number)) {
            System.out.println("The number " + number + " has been found!!!");
        } else {
            System.out.println("The number " + number + " doesn't exist");
        }

    }

    // Print method to display the element of the array
    private static void print(double m_array[]) {
        for (int i = 1; i <= m_array.length; i++) {
            System.out.print(m_array[i-1] + " ");
            if (i % 10 == 0) {
                System.out.println();
            }
        }
    }

    private static boolean hasElement(double m_array[], double element) {
        for (double e : m_array) {
            if (e == element) {
                return true;
            }
        }
        return false;
    }
}
相关问题