简体   繁体   English

在Java中打印数字的模式序列

[英]Print a pattern sequence of numbers in Java

I need to write a method called printOnLines() that takes two arguments an integer n and an integer inLine and prints the n integer, every inLine on a line. 我需要编写一个名为printOnLines()的方法,该方法接受两个参数:整数n和整数inLine,并在一行中的每个inLine上打印n个整数。

for n = 10, inLine = 3:
1, 2, 3
4, 5, 6
7, 8, 9
10

My call is (10,3) . 我的电话是(10,3) Here's my code: 这是我的代码:

public static void printOnLines(int n, int s) {
        for(int i=1; i<=s; i++) {
            for(int j=1; j<=n-1; j++) {
                System.out.print(j + ", ");
            }
        System.out.println();
        }
    }
}

I believe that there are two mistakes: 我相信有两个错误:

  1. I need to remove the last comma appearing in the output. 我需要删除输出中出现的最后一个逗号。
  2. I need to place 3 of the numbers of each line until I reach the number 10. 我需要在每一行中放置3个数字,直到达到10。

Use this: 用这个:

public static void printOnLines(int n, int s){
     StringBuilder builder = new StringBuilder();
     for(int i = 1; i <= n; i++){
         builder.append(i);
         if(i % s == 0)builder.append("\n");
         else builder.append(", ");
     }
     System.out.println(builder.toString().substring(0,builder.toString().length() - 2));
}

I'm not going to post a complete solution, since this is clearly homework. 我不会发布完整的解决方案,因为这显然是家庭作业。 But here is what I would do, if I weren't allowed to use if statements or ternary operators. 但是,如果不允许使用if语句或三元运算符,这就是我会做的事情。

  • Make the index of the outer loop start at 0 and increase by s on every iteration, instead of by 1 (So it goes 0, 3, 6, 9 in your example). 使外循环的索引从0开始,并在每次迭代中均由s而不是1所增加(因此在示例中为0、3、6、9)。
  • Print the sum of the two loop indexes on each iteration. 在每次迭代中打印两个循环索引的总和。
  • Print the last number on each line outside of the inner loop, without a comma. 在内部循环外的每一行上打印最后一个数字,不带逗号。

Edit 编辑

OK, on @localhost's request, here is my solution. OK,应@localhost的要求,这是我的解决方案。

public static void printOnLines(int n, int s) {
    for (int i = 0; i < n; i += s) {
        int j;
        for (j = 1; j < s && i + j < n; j++) {
             System.out.print(i + j + ", ");
        }
        System.out.println(i + j);
    }
}

I assume you've already had to submit your homework, so here's how I'd do it: 我认为您已经必须提交家庭作业,因此我将按照以下方式进行:

class printonlines {
public static void main(String[] args) {        
    printOnLines(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
}

public static void printOnLines(int n, int s) {     
    for(int i=1; i<=n; i++) {  // do this loop n times
        if ( (i != 1) && (i-1)%s == 0 ) {  // if i is exactly divisible by s, but not the first time
            System.out.println(); // print a new line
        }
        System.out.print(i);
        if (i != n) {  // don't print the comma on the last one
            System.out.print(", ");
        }
    }
    }
}

Oh, you don't want to use any if statements. 哦,您不想使用任何if语句。 All you really learn from doing "tricks" like what you are asked to do is a single trick, not how to write easily comprehensible (even by yourself when you have to debug later) code. 像要求您做的那样,从“技巧”中真正学到的只是一个技巧,而不是如何编写容易理解的代码(即使您自己需要稍后调试时也是如此)。

Anyway, here's my less comprehensible but more tricky code that doesn't use if as per the suggestions from the answer from @DavidWallace. 无论如何, if按照@DavidWallace的答案的建议,这是我不那么容易理解但更棘手的代码。 I'm sure someone can do it neater but this is the best I could do in 15 minutes... 我敢肯定有人可以做得更整洁,但这是15分钟内我能做的最好的事情。

class printonlineswithnoifs {
    public static void main(String[] args) {

        printOnLines(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
    }

    // i 012012012
    // j 000333666
  // i+j 012345678

    public static void printOnLines(int n, int s) {

        int i = 0;
        int j = 1;
        for (; j <= n; j+=s) {
            for (; i < (s-1) && (j+i)<n; i++) {

                 System.out.print((i+j) + ", ");
            }
            System.out.println(i+j);
            i=0;
        }
    }
}

Here is the code sample you wanted.. 这是您想要的代码示例。

public static void printOnLines(int n, int s) {
    for (int i = 1; i < n; i++) {
        System.out.print(i + ",\t");
        if (i % 3 == 0) System.out.println();
    }
    System.out.println(n);
}

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

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