在每行上打印一个包含10个元素的数组元素 [英] Print an array elements with 10 elements on each line

查看:149
本文介绍了在每行上打印一个包含10个元素的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了一个包含100个初始化值的数组,我想在每行打印出10个元素,所以它就像这样......

I just created an array with 100 initialized values and I want to print out 10 elements on each line so it would be somthing like this

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
...26

这是我使用的代码,我设法为前10个元素做了但我无法弄清楚如何为其余元素做这个

this is the code I used and I managed to do it for the first 10 elements but I couldn't figure out how to do it for the rest

public static void main(String[] args) {

    int[] numbers = { 0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};  
    int i, count = 0;

    for (i = 0; i < numbers.length; i++) {

        System.out.print(numbers[i] + " ");
        count++;

        if (count == 9)
            for (i = 9; i < numbers.length; i++)
                System.out.println(numbers[i] + " ");
    }
}


推荐答案

    int[] numbers = new int[100];
    for (int i = 0; i < numbers.length; i++) {
        if (i % 10 == 0 && i > 0) {
            System.out.println();
        }
        System.out.print(numbers[i] + " ");
    }

打印前打印换行符 numbers [i] 其中 i%10 == 0 i> 0 是mod运算符;如果 i / 10 ,则返回余数。所以 i%10 == 0 i = 0,10,20,...

This prints a newline before printing numbers[i] where i % 10 == 0 and i > 0. % is the mod operator; it returns the remainder if i / 10. So i % 10 == 0 when i = 0, 10, 20, ....

至于原始代码,您可以按照以下步骤进行一些修改:

As for your original code, you can make it work with a little modification as follows:

int count = 0;
for (int i = 0; i < numbers.length; i++) {
   System.out.print(numbers[i] + " ");
   count++;
   if (count == 10) {
     System.out.println();
     count = 0;
   }
}

基本上, count 是您在此行中打印的数字。一旦达到10,则打印换行符,然后将其重置为0 ,因为您正在开始一个新行,而对于该行,您还没有打印任何数字(尚未)。

Basically, count is how many numbers you've printed in this line. Once it reaches 10, you print the newline, and then reset it back to 0, because you're starting a new line, and for that line, you haven't printed any numbers (yet).

请注意,在上述两种解决方案中,每行末尾都会打印一个额外的空格。这是一个更灵活的实现,只在必要时才使用分隔符(水平和垂直)。只有稍微更复杂。

Note that in above two solutions, an extra space is printed at the end of each line. Here's a more flexible implementation that only uses separators (horizontal and vertical) when necessary. It's only slightly more complicated.

static void print(int[] arr, int W, String hSep, String vSep) {
    for (int i = 0; i < arr.length; i++) {
        String sep =
            (i % W != 0) ? hSep :
            (i > 0)      ? vSep :
            "";
        System.out.print(sep + arr[i]);
    }
    System.out.println(vSep);
}

如果你这么说,就像 print(new int [25],5,,,。\ n); ,然后它将打印25个零,每行5个。每行末尾有一段时间(),并且行之间的零之间有一个逗号() 。

If you call this say, as print(new int[25], 5, ",", ".\n");, then it will print 25 zeroes, 5 on each line. There's a period (.) at the end of each line, and a comma (,) between zeroes on a line.

这篇关于在每行上打印一个包含10个元素的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆