Java 打印多行数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22179557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printing an array in multiple lines
提问by user3380238
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
我正在上编程课,但事情对我来说并没有点击。我有一个任务要求:
Write a program to assign the integer values 1 through 25 to a 25 element integer array. Then, print the array as five separate lines each containing five elements separated by commas. The last element on each line should be followed by a newline instead of a comma. The output of your program should appear exactly as follows:
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
Hints:
- One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is 0, it is the 5th number.
- You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same line.
编写一个程序,将整数值 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() 打印一个没有换行符的值。这将允许您在同一行上打印多个内容。
I have a little bit of code but I don't know where to go from here:
我有一些代码,但我不知道从哪里开始:
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();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
我只是对编程有一个心理障碍 - 谁能帮我指出一些有用的例子?
采纳答案by Vishal Santharam
Try this,
尝试这个,
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]);
}
}
}
回答by Adam
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(", ");
}
}
}
回答by Alex Kartishev
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(", ");
}
}
}
}
And try to learn Java syntax first of all.
并首先尝试学习 Java 语法。
回答by Xabush
Here is an enhanced version of your code.
这是您的代码的增强版本。
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 + ",");
}
}
}
}
}