运用Java数组 编程在屏幕上输出如下图所示的数字斜塔
发布网友
发布时间:2024-10-23 21:06
我来回答
共4个回答
热心网友
时间:2024-10-23 21:23
我的方法数组大小可以随便哦,
/**
* @param args
*/
public static void main(String[] args) {
String a[][] = new String[5][5];
int m = 0;
for (int i = 0; i < 5; i++) {
int n = 1;
for (int j = 0; j < 5; j++) {
if (i == 0&&j==0) {
a[i][j] = String.valueOf(1);
}
else if (i == 0&&j != 0){
a[i][j] = String.valueOf(Integer.valueOf(a[i][j - 1]) + n);
}
else if (i != 0&&j==0) {
a[i][j] = String.valueOf(Integer.valueOf(a[i-1][j])+m);
}
else{
a[i][j] = String.valueOf(Integer.valueOf(a[i][j - 1]) + n+m);
}
n++;
//打印
if (j == 4) {
if (Integer.valueOf(a[i][j])>15) {
System.out.println(" " + " ");
}
else {
System.out.println(a[i][j] + " ");
}
} else {
if (Integer.valueOf(a[i][j])>15) {
System.out.print(" " + " ");
}
else {
System.out.print(a[i][j] + " ");
}
}
}
m++;
}
}
热心网友
时间:2024-10-23 21:20
Dim strArr() As String
Dim strOut As String
Dim strInt As String
strInt = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
strArr = Split(strInt, ",")
Dim j As Integer
For j = 1 To 15
If (j - 1) * j / 2 + 1 < 15 Then
If strOut <> "" Then
strOut = strOut & vbCrLf
End If
Dim i As Integer
For i = j To 15
If i * (i + 1) / 2 - j <= 15 Then
strOut = strOut + " " & strArr(i * (i + 1) / 2 - j)
End If
Next i
End If
Next j
MsgBox strOut
vba写的,我这里没eclipse,两个for循环嵌套而已
热心网友
时间:2024-10-23 21:23
public static void main(String[] args) {
final int num = 5;
int[][] array = new int[num][num];
for (int i = 0; i < num; i++) {
int j = 0;
if (i == 0 && j == 0) {
array[i][j] = 1;
} else {
array[i][j] = array[i - 1][j] + i;
}
}
for (int j = 1; j < num; j++) {
for (int i = 0; i < num - j; i++) {
array[i][j] = array[i + 1][j - 1] + 1;
}
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (j < num - i) {
System.out.print(array[i][j] + " ");
}
}
System.out.println("");
}
}
热心网友
时间:2024-10-23 21:22
不太明白用意,如果是单纯用数组
直接往数组里存入
1,3,6,10,15,0,2,5,9,14,0...
这样的数组,然后循环遍历,遇到0就换行不行吗?
还是说写个循环,往数组里放入这样的数字,然后再输出出来?
一样的道理,就是把手动写入数字部分换成循环逻辑
Array number = new Array();
int num = 1;
for (int j = 0; j < 5; j++)
{
num = num + j;
int printNum = num;
number.add(printNum);
for (int i = j+1; i < 5; i++)
{
printNum = printNum + (i + 1);
number.add(printNum);
}
number.add(0);
}
然后循环遍历输出,遇到0就换行