
要求就是编写一个带状矩阵,代入数值之后需要运行出相应的带状矩阵
import java.util.Scanner;
public class test {
public static void bandMatrix(int n, int width) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Math.abs(i - j) <= width)
System.out.print("*");
else
System.out.print("0");
if (j != n - 1)
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("plase input n and width:");
while (sc.hasNextInt()) {
int n = sc.nextInt();
int width = sc.nextInt();
bandMatrix(n, width);
System.out.print("plase input n and width:");
}
sc.close();
// bandMatrix(4, 1);
// bandMatrix(4, 2);
}
}