如何显示N正方形图案的N数字1到9,然后在Java中显示0



我已经尝试将值放在数组中,使用矩阵,甚至使用递归数组,但这些数组仅返回错误。

现在我制作的代码看起来像

import java.util.*;
public class SQf1t9t0 {
    int n, i, j;   
    Scanner yar = new Scanner(System.in);
    System.out.println("Enter size for n square: ");
    n = yar.nextInt();
    int[][] f19t0 = new int[n][n];
    for (i = 0; i < n; i++) 
    {
        for (j = 0; j < n; j++) 
        {
            f19t0[i][j] = i+1;
            System.out.print(f19t0[i][j] + " ");
        }
        System.out.println();
    }
}

例如,如果n的输入值是4:

,则必须像这样。
1234
5678
9012

我真的不认为您仅需要数组才能打印值。您可以只有一次柜台并休息一次。9。

int counter = 0;
for (i = 0; i < n; i++) 
{
  for (j = 0; j < n; j++) 
  {
    System.out.print(counter);
    if(++counter > 9) {
      counter = 0;
    }
  }
  System.out.println();
}

您只需要使用modulo在9之后重新启动。

import java.util.Scanner;

public class DisplayMatrix
{
    public static void main(String[] args) {
        int n, i, j;
        Scanner yar = new Scanner(System.in);
        System.out.println("Enter size for n square: ");
        n = yar.nextInt();
        int[][] f19t0 = new int[n][n];
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                f19t0[i][j] = (i * n + j + 1) % 10;
                System.out.print(f19t0[i][j] + " ");
            }
            System.out.println();
        }
        yar.close();
    }
}

例如:

Enter size for n square: 
6
1 2 3 4 5 6 
7 8 9 0 1 2 
3 4 5 6 7 8 
9 0 1 2 3 4 
5 6 7 8 9 0 
1 2 3 4 5 6 

ps:不要忘记关闭扫描仪。

您的课程中没有方法。我不知道您是否没有错误地将其放置!但是以下奏效。

import java.util.*;
public class test {
    public static void main(String[] args) {
        int n, i, j;
        Scanner yar = new Scanner(System.in);
        System.out.println("Enter size for n square: ");
        n = yar.nextInt();
        int[][] f19t0 = new int[n][n];
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                f19t0[i][j] = (i*n+j+1) % 10;
                System.out.print(f19t0[i][j] + " ");
            }
            System.out.println();
        }
        yar.close();
    }
}

对于输入5,它输出:

Enter size for n square: 
5
1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5 

最新更新