我需要幫助打印數組,我需要每行打印6個項目並切換到第七個和後續數字的下一行。還有誰會將數字輸入數組而不定義輸入多少個數字?在特定行上打印數組
import java.util.Scanner;
public class NumberArray
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many grades do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " grades now.");
for (int grades = 0 ; grades < array.length; grades++)
{
array[grades] = input.nextInt();
}
System.out.println("These are the grades you have entered.");
printArray(array);
}
public static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " \t");
}
}
}
如果你想看看你是否在第7行,只需檢查你的迭代器是否爲7.如果你想看看你是否在7的倍數,然後測試modula(%)7 == 0 – Chris 2014-11-02 22:24:09
println會打印並進入下一行,打印將不會...(除非您打印回車) – Chris 2014-11-02 22:24:25