简体   繁体   English

使用循环在Java中创建表

[英]Making a table in Java using loops

I have to make a table in Java using loops. 我必须使用循环在Java中创建一个表。 It needs to print out 1-78 in one column, then 15-75 every five times (eg 15, 20, 55, ... 75) 78 times(as the loop needs to print out a total of 78 values; so the loop needs to iterate 6 times) in the second column, then print out 15-30 every three times (eg 15, 18, 21, 24 , ...30)78 times (as the loop needs to print out a total of 78 values; so the loop needs to iterate 13 times), in the third column and then the number four in the fourth column. 它需要在一列中打印出1-78,然后每五次打印15-75(例如15、20、55,... 75)78次(因为循环总共需要打印出78个值;因此循环需要在第二列中进行6次迭代,然后每三遍打印15-30次(例如15、18、21、24,... 30)78次(因为循环总共需要打印78次)值;因此循环需要迭代13次),在第三列中进行迭代,然后在第四列中进行四次迭代。 This is what I have so far: 这是我到目前为止的内容:

import java.util.Scanner;
public class Sandbox {


    public static void main(String[] args) { //main method
        int angles = 10; 
        double  cosines =4;
        int meters =12;
        for (int i =1; i<=78;i++){
            System.out.println(i);
        }
        for (int j = 1; j <=6; j++) {
            for (angles =10 ;angles<75;) {
                angles +=5;
                for (meters=12; meters <30;) {
                    meters +=3;
                    int num1= 4;
                    System.out.print( ")" +angles + "|"+ meters) +4);
                }
            }
        }

This outputs the correct values but prints them in a way such that it will be 1-78 then meters = 30 then the values of 15-75 78 times. 这将输出正确的值,但以如下方式打印它们:1-78,然后米= 30,然后是15-75,即78次。

This I think does what you want: 我认为这是您想要的:

public static void main(String[] args) { //main method
    int angles = 15;
    double  cosines =4;
    int meters =15;
    for (int i =1; i<=78;i++){
        System.out.print(i);
        System.out.print('|');
        System.out.print(angles);
        angles+=5;
        if (angles>75){
            angles=15;
        }
        System.out.print('|');
        System.out.print(meters);
        meters+=3;
        if (meters>30){
            meters=15;
        }
        System.out.println("|4|");
    }

}

You're confused about all the numbers of loops, the thing is you need to generate 78 rows, to you need to loop 78 times, full stop. 您对所有循环次数感到困惑,问题是您需要生成78行,而您需要循环78次,即句号。 Then for each loop you print your variables, increment and reset them as needed, and loop back. 然后,对于每个循环,您将打印变量,根据需要递增和重置它们,然后循环返回。

A possible solution is to build the entire output in a 78-by-4 (or 4-by-78) array first, then printing it all in two nested loops. 一种可能的解决方案是首先将整个输出构建为78×4(或4×78)阵列,然后将其全部打印在两个嵌套循环中。 If that is easier for you or feels more natural. 如果这样对您来说更简单或更自然。 This way you could reuse the idea and much of the code in your original program. 这样,您就可以在原始程序中重用该想法和许多代码。

public static void main(String[] args) {
    int[][] table = new int[4][78];

    // generate table
    int angles = 10;
    double cosines = 4;
    int meters = 12;
    int line = 0;
    for (int i = 1; i <= 78; i++) {
        table[0][line] = i;
        line++;
    }
    line = 0;
    for (int j = 1; j <= 6; j++)
        for (angles = 10; angles < 75;) {

            angles += 5;
            table[1][line] = angles;
            line++;
        }
    line = 0;
    for (int j = 1; j <= 13; j++)
        for (meters = 12; meters < 30;) {
            meters += 3;
            table[2][line] = meters;
            table[3][line] = 4;
            line++;
        }

    // print table
    for (line = 0; line < 78; line++) {
        System.out.print(table[0][line]);
        for (int column = 1; column < 4; column++) {
            System.out.print("|" + table[column][line]);
        }
        System.out.println();
    }
}

What you need is to build the input inside of the overall loop, assuming you want a table like 您需要的是在整个循环中构建输入,假设您想要一个像

1 | 15 20 25 30 ... 75 | 15 18 21 ... 30 | 4
2 | 15 20 25 30 ... 75 | 15 18 21 ... 30 | 4
....

In order to build this up, you'll need nested loops and output. 为了构建它,您需要嵌套的循环和输出。 It can be quite simple as long as you make the loops simple. 只要使循环简单即可,它可能非常简单。

for (int i = 1; i <= 78; i++)
{
    System.out.print(i + " | ");

    for (int j = 15; j <=75; j += 5)
        System.out.print(j + " ");

    System.out.print("| ");

    for (int k = 15; k <= 30; k += 3)
        System.out.print(k + " ");

    System.out.print("| 4 |");

    System.out.println("");
}

As Kira Yakuza mention in the comments , you would be better off dividing this into methods. 正如Kira Yakuza在评论中提到的那样,最好将其划分为方法。

Note that your question is somewhat unclear on the sample output, and this is how I interpreted what you wrote. 请注意,您的问题在示例输出中还不清楚,这就是我解释您所写内容的方式。 If you desire a different output, you should edit your question to clarify. 如果您希望获得不同的输出,则应编辑问题进行澄清。

You can calculate the value given the index, max, factor, and offset for the type of data. 您可以根据给定的数据类型的索引,最大值,因数和偏移量来计算值。 With this information, you can generate each row, no matter which index you are at. 利用此信息,无论您位于哪个索引,都可以生成每一行。

TablePrinter.java TablePrinter.java

public class TablePrinter {
    public static interface RowPrinter {
        String print(int rowIndex, int dataIndex, Object[][] data);
    }

    public static void main(String[] args) {
        // { label, max, factor, offset }
        Object[][] data = {
            { "angles", 75, 5, 10 },
            { "meters", 30, 3, 12 }
        };

        printTable(data, new RowPrinter() {
            @Override
            public String print(int rowIndex, int dataIndex, Object[][] data) {
                int max = (int) data[dataIndex][1];
                int factor = (int) data[dataIndex][2];
                int offset = (int) data[dataIndex][3];
                return String.format(" | %8d", calculate(rowIndex, max, factor, offset));
            }
        });
    }

    public static void printTable(Object[][] data, RowPrinter rowPrinter) {
        // Print headers
        System.out.printf("%8s", "index");
        for (int i = 0; i < data.length; i++) {
            System.out.printf(" | %8s", data[i][0]);
        }
        System.out.println();

        // Print data rows
        for (int i = 0; i < 75; i++) {
            System.out.printf("%8d", i + 1);
            for (int j = 0; j < data.length; j++) {
                System.out.printf(rowPrinter.print(i, j, data));
            }
            System.out.println();
        }
    }

    public static int calculate(int index, int max, int factor, int offset) {       
        return ((index * factor) % (max - offset + factor)) + offset;
    }
}

Result 结果

   index |   angles |   meters
       1 |       10 |       12
       2 |       15 |       15
       3 |       20 |       18
       4 |       25 |       21
       5 |       30 |       24
       6 |       35 |       27
       7 |       40 |       30
       8 |       45 |       12
       9 |       50 |       15
      10 |       55 |       18
      11 |       60 |       21
      12 |       65 |       24
      13 |       70 |       27
      14 |       75 |       30
      15 |       10 |       12
      16 |       15 |       15
      17 |       20 |       18
      18 |       25 |       21
      19 |       30 |       24
      20 |       35 |       27
      21 |       40 |       30
      22 |       45 |       12
      23 |       50 |       15
      24 |       55 |       18
      25 |       60 |       21
      26 |       65 |       24
      27 |       70 |       27
      28 |       75 |       30
      29 |       10 |       12
      30 |       15 |       15
      31 |       20 |       18
      32 |       25 |       21
      33 |       30 |       24
      34 |       35 |       27
      35 |       40 |       30
      36 |       45 |       12
      37 |       50 |       15
      38 |       55 |       18
      39 |       60 |       21
      40 |       65 |       24
      41 |       70 |       27
      42 |       75 |       30
      43 |       10 |       12
      44 |       15 |       15
      45 |       20 |       18
      46 |       25 |       21
      47 |       30 |       24
      48 |       35 |       27
      49 |       40 |       30
      50 |       45 |       12
      51 |       50 |       15
      52 |       55 |       18
      53 |       60 |       21
      54 |       65 |       24
      55 |       70 |       27
      56 |       75 |       30
      57 |       10 |       12
      58 |       15 |       15
      59 |       20 |       18
      60 |       25 |       21
      61 |       30 |       24
      62 |       35 |       27
      63 |       40 |       30
      64 |       45 |       12
      65 |       50 |       15
      66 |       55 |       18
      67 |       60 |       21
      68 |       65 |       24
      69 |       70 |       27
      70 |       75 |       30
      71 |       10 |       12
      72 |       15 |       15
      73 |       20 |       18
      74 |       25 |       21
      75 |       30 |       24

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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