英文:
how do I get my diagonal line to start printing from the bottom left of my asterik box?
问题
我正在编写一个代码,用于练习创建方法并在主方法中调用这些方法。我在输出方面遇到了问题,输出结果与预期不符。代码应该根据输入的数字9创建一个方框。以下是我的代码输出:
*********
*+ *
* + *
* + *
* + *
* + *
* + *
* +*
*********
我无法弄清楚如何使斜线“+”从方框的左下角开始,到达右上角。我的代码如下:
public static void main(String[] args) {
System.out.print(boxWithMinorDiagonal(9));
}
public static int boxWithMinorDiagonal(int n) {
for (int col = 1; col <= n; col++) {
for (int row = 1; row <= n; row++) {
if (row == 1 || col == 1 || row == n || col == n) {
System.out.print("*");
} else if (col == row) {
System.out.print("+");
} else {
System.out.print(" ");
}
}
System.out.println();
}
return n;
}
我认为我需要让行(row)和列(col)从左下角开始,从那里开始斜线,但我不确定我需要在for循环中做哪些更改,以使行和列从左下角开始。
英文:
I am making a code that is to practice for creating methods and calling them in the main method. I am having trouble with an output that is not exactly how it is supposed to look like. The code is supposed to create a box based on the input 9. This is the output of my code:
*********
*+ *
* + *
* + *
* + *
* + *
* + *
* +*
*********
I can't figure out how to get the "+" diagonal to start from the bottom left of the box to the top right. my code is this:
public static void main(String[] args)
{
System.out.print(boxWithMinorDiagonal(9));
}
public static int boxWithMinorDiagonal(int n){
for (int col = 1; col <= n; col++) {
for (int row = 1; row <= n; row++) {
if(row == 1 || col == 1 || row == n || col == n){
System.out.print("*");
}
else if (col == row){
System.out.print("+");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
return n;
}
I think I need to get the row and col to start from the bottom left, which would start the diagonal from there, but am not exactly sure what I would need to change in my for loops to get the row and col to start from the bottom left.
答案1
得分: 1
现在,如果例如 col
为 1 且 row
为 1,或者 col
为 2 且 row
为 2,你会绘制一个星星。
制作一个表格,列出应该在从左下到右上方向上产生星星的行/列组合,然后尝试找出其中的规律,可以用数学运算的方式来表示,以返回一个布尔值。
提示:col + row == something
可能是你想要查看的内容。
英文:
Right now you draw a star if e.g. the col
is 1 and the row
is 1, or the col
is 2 and the row
is 2.
Make a table of which row/col combos should produce a star for bottom-left to top-right, then try to find a pattern in this you can put in terms of a mathematical operation that returns a boolean.
HINT: col + row == something
might be something you want to check out.
答案2
得分: 0
只需更改这部分代码:else if (col == n-row+1){ System.out.print("+");}
那么你的代码将变为:
{
System.out.print(boxWithMinorDiagonal(9));
}
public static int boxWithMinorDiagonal(int n){
for (int col = 1; col <= n; col++) {
for (int row = 1; row <= n; row++) {
if(row == 1 || col == 1 || row == n || col == n){
System.out.print("*");
}
else if (col == n-row+1){
System.out.print("+");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
return n;
}
然后你将获得以下输出:
*********
* +*
* + *
* + *
* + *
* + *
* + *
*+ *
*********
9
英文:
just change this part :else if (col == n-row+1){ System.out.print("+");}
So your code will be :
{
System.out.print(boxWithMinorDiagonal(9));
}
public static int boxWithMinorDiagonal(int n){
for (int col = 1; col <= n; col++) {
for (int row = 1; row <= n; row++) {
if(row == 1 || col == 1 || row == n || col == n){
System.out.print("*");
}
else if (col == n-row+1){
System.out.print("+");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
return n;
and you'll get this:
*********
* +*
* + *
* + *
* + *
* + *
* + *
*+ *
*********
9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论