i am preparing my self for the interview. I have to make a equilateral triangle Using 7X7 2D array.
我正在为面试做准备。我必须使用7X7 2D阵列制作等边三角形。
I tried the below code but i am confused like what should i do in my code so that i can make a equilateral triangle?
我尝试了下面的代码,但我很困惑,我应该怎么做我的代码,以便我可以做一个等边三角形?
package learn.basic.corejava;
public class ArrayExample {
public void two_equilateral_D()
{
System.out.println("Demonstrating 2D 3X3 Equal trngle");
int twoD[][]=new int[7][7];
int i,j,k=0;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
twoD[i][j]=k>9?0:k;
k++;
}
}
for(i=0;i<7;i++)
{
for(j=i+3;j<7;j++) {
System.out.print(twoD[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String args[] )
{
ArrayExample obj=new ArrayExample();
//System.out.println("Average value of the array="+obj.calculate_average());
obj.two_equilateral_D();
}
}
But this code gives me output like
但是这段代码给了我类似的输出
Demonstrating 2D 3X3 Equal trngle
3 4 5 6
0 0 0
0 0
0
Desired output like this
期望的输出像这样
3 个解决方案
#1
0
To approach your problem, you should start by breaking it down into two smaller sub-problems:
要解决您的问题,您应该首先将其分解为两个较小的子问题:
-
Generate the desired triangular matrix of values (it's called Pascal's triangle, among other things).
生成所需的三角形值矩阵(其中称为Pascal的三角形)。
-
Display the values nicely.
很好地显示值。
Each of the problems is fairly straightforward by itself. Try your hand at them and come back if you need help.
每个问题本身都相当简单。试试你的手,如果你需要帮助,请回来。
#2
0
I think the special condition (the hard part) shouldn't be in the loop condition, but in a new condition inside the nested loop.
我认为特殊条件(硬部分)不应该处于循环条件中,而是处于嵌套循环内的新条件中。
-
You need to iterate over the whole matrix, instead of limiting the nested loop (with variable
j
).您需要迭代整个矩阵,而不是限制嵌套循环(使用变量j)。
-
Inside the loop, add an
if()
statement to check whether you want to display the value or just a space. This condition is what you should work on.在循环内部,添加if()语句以检查是要显示值还是仅显示空格。这种情况是你应该做的。
The code should look like this:
代码应如下所示:
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (condition) {
System.out.print(twoD[i][j]+" ");
} else {
System.out.print(" "); // note the extra space here to make up for a number
}
}
System.out.println("");
}
EDIT: You radically changed your question, there is now two problems:
编辑:你从根本上改变了你的问题,现在有两个问题:
-
You have to initialize your matrix with Pascal's Triangle values.
您必须使用Pascal的三角形值初始化矩阵。
-
The output you now posted is not a console output, you'll have to make choices as to where to put the spaces, and how to deal with 2-digit numbers. The easiest is to make every cell take 2 characters (either a space and a digit, or 2 digits).
您现在发布的输出不是控制台输出,您必须选择放置空格的位置以及如何处理2位数字。最简单的方法是使每个单元格占用2个字符(空格和数字,或2位数字)。
#3
-1
in the second nested loop
在第二个嵌套循环中
for(i=0;i<7;i++)
{
for(j=i+3;j<7;j++) {
System.out.print(twoD[i][j]+" ");
}
System.out.println("");
}
you are removing any values beyond index 3 which is why you are only going to 4 numbers instead of 7
你正在删除索引3之外的任何值,这就是为什么你只需要4个数而不是7个
After writing to the 7x7 array use
写入7x7阵列后使用
int num=0;
for(i=1;i<7;i++)
{
num=0;
for(j=7;j>7-i;j--)
{
System.out.print(twoD[i][j]+" ");
System.out.print(twoD[i][num]+" ");
num++;
}
System.out.println("");
}
This should give the desired output
这应该给出所需的输出
#1
0
To approach your problem, you should start by breaking it down into two smaller sub-problems:
要解决您的问题,您应该首先将其分解为两个较小的子问题:
-
Generate the desired triangular matrix of values (it's called Pascal's triangle, among other things).
生成所需的三角形值矩阵(其中称为Pascal的三角形)。
-
Display the values nicely.
很好地显示值。
Each of the problems is fairly straightforward by itself. Try your hand at them and come back if you need help.
每个问题本身都相当简单。试试你的手,如果你需要帮助,请回来。
#2
0
I think the special condition (the hard part) shouldn't be in the loop condition, but in a new condition inside the nested loop.
我认为特殊条件(硬部分)不应该处于循环条件中,而是处于嵌套循环内的新条件中。
-
You need to iterate over the whole matrix, instead of limiting the nested loop (with variable
j
).您需要迭代整个矩阵,而不是限制嵌套循环(使用变量j)。
-
Inside the loop, add an
if()
statement to check whether you want to display the value or just a space. This condition is what you should work on.在循环内部,添加if()语句以检查是要显示值还是仅显示空格。这种情况是你应该做的。
The code should look like this:
代码应如下所示:
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (condition) {
System.out.print(twoD[i][j]+" ");
} else {
System.out.print(" "); // note the extra space here to make up for a number
}
}
System.out.println("");
}
EDIT: You radically changed your question, there is now two problems:
编辑:你从根本上改变了你的问题,现在有两个问题:
-
You have to initialize your matrix with Pascal's Triangle values.
您必须使用Pascal的三角形值初始化矩阵。
-
The output you now posted is not a console output, you'll have to make choices as to where to put the spaces, and how to deal with 2-digit numbers. The easiest is to make every cell take 2 characters (either a space and a digit, or 2 digits).
您现在发布的输出不是控制台输出,您必须选择放置空格的位置以及如何处理2位数字。最简单的方法是使每个单元格占用2个字符(空格和数字,或2位数字)。
#3
-1
in the second nested loop
在第二个嵌套循环中
for(i=0;i<7;i++)
{
for(j=i+3;j<7;j++) {
System.out.print(twoD[i][j]+" ");
}
System.out.println("");
}
you are removing any values beyond index 3 which is why you are only going to 4 numbers instead of 7
你正在删除索引3之外的任何值,这就是为什么你只需要4个数而不是7个
After writing to the 7x7 array use
写入7x7阵列后使用
int num=0;
for(i=1;i<7;i++)
{
num=0;
for(j=7;j>7-i;j--)
{
System.out.print(twoD[i][j]+" ");
System.out.print(twoD[i][num]+" ");
num++;
}
System.out.println("");
}
This should give the desired output
这应该给出所需的输出