繁体   English   中英

多行打印数组

[英]printing an array in multiple lines

我正在上一门编程课,而事情对我而言并不是一件容易的事。 我有一个作业要求:

编写一个程序,将1到25的整数值分配给25个元素的整数数组。 然后,将数组打印为五行,每行包含五个用逗号分隔的元素。 每行的最后一个元素应后跟换行符而不是逗号。 程序的输出应完全如下所示:

 1,2,3,4,5 6,7,8,9,10 11,12,13,14,15 16,17,18,19,20 21,22,23,24,25 

提示:

  • 确定第5个元素的一种方法是使用模块运算符(%)。 如果将下标除以5,而余数为0,则它​​是第5个数字。
  • 您可以使用System.out.print()来打印值,而后没有换行符。 这将允许您在同一行上打印多个内容。

我有一些代码,但是我不知道从这里去哪里:

public class Program4

{

     public static int[] array;

     public static void main(String[] args);

     {

          int[] numbers = new int [25]

          for(int i=0; i<25; i++)

                array[i] = i + 1;}

     public static void printArray()

     {

          for(int i=1; i<=25; i++);

          {

               System.out.print(array[i - 1]);

               if (i % 5 == 0)

                    System.out.printIn();

           }

     }

我只是对编程有一个心理障碍-有人可以帮我指出一些有用的示例吗?

尝试这个,

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
     public static int[] array;

     public static void main(String[] args)
     {
        array = new int[25];
        for(int i=0; i<25; i++)
           array[i] = i + 1;   
        printArray();
     }

     public static void printArray()

     {
        int i;
        for(i=1; i<=25; i++){
            if (i % 5 != 0)
                System.out.print(array[i-1]+",");
            else
                System.out.println(array[i-1]);
       }
     }
}
public class Foo {

    public static int[] nums;

    public static void main(String[] args) {
        nums = new int[25];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i + 1;
        }
        printArray(nums);
    }

    public static void printArray(int[] myArray) {
        for (int i = 0; i < myArray.length; i++) {
            System.out.print(String.valueOf(myArray[i]);
            if (i % 5 == 0) {
                System.out.println();
            } else if (i % 5 != 4){
                System.out.println(", ");
        }
    }
}
public class Test {
    public static void main(String[] args) {

        int[] array = new int [25];

        for(int i=0; i<25; i++) {
            array[i] = i + 1;
        }


        for (int i=1; i<=25; i++) {
            System.out.print(array[i - 1]);
            if (i % 5 == 0) {
                System.out.println();
            } else {
                System.out.print(", ");
            }
        }
    }
}

并首先尝试学习Java语法。

这是您代码的增强版本。

public class Program4

{

public static int[] array = new int[25];//instantiate the array to its default values

public static void main(String[] args)
{
    //calling the methods from main
    addToArray();
    printArray();
}
//add the numbers to the array
public static void addToArray(){



    for(int i=0; i<25; i++)

        array[i] = i + 1;

}

 //print the numbers from the array

public static void printArray()
{
  for(int i = 1; i <= 25; i++){
      if(i % 5 == 0){
          System.out.print(i);
          System.out.println();
      }
      else{
          System.out.print(i + ",");
      }
  }

}

}

暂无
暂无

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

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