如何制作索引表

时间:2014-08-11 20:56:19

标签: java list printing

在我的代码中,我有几个打印语句,我想安排到一个表中。打印语句的长度根据用户输入的数字而变化。我把代码放到了只需要将自己正确地安排到表中的地方。

以下是一些无法正常运行的代码:

import java.util.Scanner;

public class DataAnalyze {

public static void main(String[] args) {
    int  i = 0,  SS = 0, Count = 0, ave1 = 0,ave2=0,ave3=0,ave4=0;  
    int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
    int max = 0, max1 = 0, max2 = 0, max3 = 0, max4 = 0;
    int min = 0, min1=0,min2=0,min3=0,min4=0;
    int [] arr1;
    int [] arr2;
    int [] arr3;
    int [] arr4;



    // Declaring variables
    Scanner input = new Scanner(System.in);
    // Creating scanner
    System.out.print("Please enter the sample size: ");
    SS = input.nextInt();
    arr1 = new int [SS];
    arr2 = new int [SS];
    arr3 = new int [SS];
    arr4 = new int [SS];
    // Finding sample size
    System.out.println("Enter numbers for Trial: ");


    for (i = 0; i < SS; i++) {

        System.out.print("Enter sample " + Count + ": ");
        arr1[i] = input.nextInt();
        Count++;
        sum1 += arr1[i];
    }
    ave1 = sum1/Count;
    System.out.println("Enter numbers for Trial: " );
    int Count1 = 0;
    for (i = 0; i < SS; i++) {

        System.out.print("Enter sample " + Count1 + ": ");
        arr2[i] = input.nextInt();
        Count1++;
        sum2 += arr2[i];
    }
    ave2 = sum2/Count1;
    System.out.println("Enter numbers for Trial: ");
    int Count11 = 0;
    for (i = 0; i < SS; i++) {

        System.out.print("Enter sample " + Count11 + ": ");
        arr3[i] = input.nextInt();
        Count11++;
        sum3 += arr3[i];
    }
    ave3 = sum3/Count11;
    System.out.println("Enter numbers for Trial: ");
    int Count111 = 0;
    for (i = 0; i < SS; i++) {

        System.out.print("Enter sample " + Count111 + ": ");
        arr4[i] = input.nextInt();
        Count111++;
        // I couldn't get Count to reset to 0 so I made them all different variables to keep everything accurate.
        sum4 += arr4[i];
    }
    ave4 = sum4/Count111;
    System.out.println("\tSample #\tTrial 1\tTrial 2\tTrial 3\tTrial 4");
    for ( i = arr1.length ; i > 0; i--) {
        System.out.println("\t"+(arr1.length-i) +  " \t\t"+ arr1[arr1.length-i] );

    }
    for ( i = arr2.length; i > 0; i--) {
        System.out.println(  "\t\t"+  " \t\t"+ arr2[arr2.length-i]);

    }
    for ( i = arr3.length; i > 0; i--) {
        System.out.println(  "\t\t"+  " \t\t\t"+ arr3[arr3.length-i]);
    }
    for ( i = arr4.length; i > 0; i--) {
        System.out.println(  "\t\t"+  " \t\t\t\t"+ arr4[arr4.length-i]);
    }
    System.out.println("---------------------------------------------------------------------------");
    System.out.println("Average: " + "\t\t" + ave1 + "\t" + ave2 + "\t" + ave3 + "\t" +ave4);


    if (ave1>ave2&&ave1>ave3&&ave1>ave4)
        max = ave1;
    if (ave2>ave1&&ave2>ave3&&ave2>ave4)
        max = ave2;
    if (ave3>ave1&&ave3>ave2&&ave3>ave4)
        max = ave3;
    if (ave4>ave1&&ave4>ave2&&ave4>ave3)
        max = ave4;

    if (ave1<ave2&&ave1<ave3&&ave1<ave4)
        min = ave1;
    if (ave2<ave1&&ave2<ave3&&ave2<ave4)
        min = ave2;
    if (ave3<ave1&&ave3<ave2&&ave3<ave4)
        min = ave3;
    if (ave4<ave1&&ave4<ave2&&ave4<ave3)
        min = ave4;

    System.out.println("Min Average: " + min);
    System.out.println("Max Average: " + max);
    if (min == max){
        System.out.print("The trials match EXACTLY!");
    }
    else if (max < min*2) {
        System.out.print("The trials concur with each other!");
    }
    else if (max > min*2) {
        System.out.print("The trials do not concur!");
    }
}}

2 个答案:

答案 0 :(得分:0)

听起来你在询问一些数组如何以每个数组成为表的列的方式打印它们。最简单的方法可能是简单地让它们迭代遍历表的行,然后对每一行迭代列值。这是一个没有足够的索引边界检查的版本。请注意,每个元素都使用打印打印,因此没有换行符,并且在打印了一行元素后,我们使用 println

public class PrintTabularExample {
    public static void main( String[] args ) {
        int[] arr1 = { 1, 2, 3 };
        int[] arr2 = { 4, 5, 6 };
        int[] arr3 = { 7, 8, 9 };

        for ( int i = 0 ; i < arr1.length; i++ ) {
            for ( int[] arr : new int[][] { arr1, arr2, arr3 } ) {
                System.out.print( "\t"+ arr[i] );   
            }
            System.out.println();
        }
    }
}

1   4   7
2   5   8
3   6   9

这是一个具有更好边界检查的版本,因此您可以打印不同长度的列表:

public class PrintTabularExample {
    public static void main( String[] args ) {
        printTabular( new Integer[][] {
                { 1, 2 },
                { 3, 4, 5, 6 },
                { 7, 8, 9 } } );
    }

    public static <T> void printTabular( T[][] elements ) {
        // Find out how many rows to print in total.
        int maxRows = 0;
        for ( T[] arr : elements ) {
            if ( arr.length > maxRows ) {
                maxRows = arr.length;
            }
        }

        // Print the elements, tabulating for each column, but 
        // only accessing a value if the subarray has enough elements.
        for ( int i = 0; i < maxRows; i++ ) {
            for( T[] arr : elements ) {
                System.out.print("\t");
                if ( i < arr.length ) {
                    System.out.print( arr[i] );
                }
            }
            System.out.println();
        }
    }
}

1   3   7
2   4   8
    5   9
    6   

答案 1 :(得分:0)

你不需要四个for循环,只需在一个循环中打印这些东西:

for (i = arr1.length; i > 0; i--) {
    System.out.println("\t" + (arr1.length - i) + " \t" + arr2[arr1.length - i] + " \t" + arr3[arr1.length - i] + " \t" + arr4[arr1.length - i]);
};

此外,一旦您的代码完美运行,您可能希望将其发布到https://codereview.stackexchange.com/,以获得有关如何使其更具可读性和可维护性的一些想法。