所以我给了它最好的一击,我真的在试图想出一个解决方案来解决这个问题。之前已经在这里询问过这类问题,但这一次有点麻烦。
所以我应该打印左箭头,例如:
public class Arrow {
public static void main(String[] args) {
int n = 0;
if (args.length < 1) {
System.out.println("Input a value.");
System.exit(0);
}
else {
n = Integer.parseInt(args[0]);
}
for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)
for (int numSpaces = 0; numSpaces < (n - rows); numSpaces++) {//Prints spaces before the '*'
System.out.print(" ");
}
for (int numStars = 0; numStars < rows; numStars++) { //Prints the " " first in each line then a "*".
System.out.print("*");
}
System.out.println(""); //Next Line
}
for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)
for (int numSpaces = n; numSpaces > (n - rows); numSpaces--) {//Prints spaces before the '*'
System.out.print(" ");
}
for (int numStars = n; numStars > rows; numStars--) { //Prints the " " first in each line then a "*".
System.out.print("*");
}
System.out.println(""); //Next Line
}
}
}
*
**
***
****
*****
****
***
**
*
现在我遇到的问题是箭头应该有一个中间部分伸出后端,相当于n +(n - 1)的长度。我无法弄清楚如何做到这一点,因为我是循环的新手,我花了2个小时的时间来试图做到这一点。
可以请一些善意的灵魂请求帮助我帮助我。
由于
答案 0 :(得分:0)
我刚刚在你的第一个大for
循环中添加了一个if块。留意// This is where the tail of the arrow starts
和// This is where the tail of the arrow ends
。我现在将尾长保持为2*n
,您可以根据需要更改它,比如说3*n
或其他内容。
if
检查有助于评估条件并采取相应措施:
for (int rows = 1; rows <= n; rows++) {//This tells how many lines to print (height)
for (int numSpaces = 0; numSpaces < (n - rows); numSpaces++) {//Prints spaces before the '*'
System.out.print(" ");
}
for (int numStars = 0; numStars < rows; numStars++) { //Prints the " " first in each line then a "*".
System.out.print("*");
}
// This is where the tail of the arrow starts
if(rows==n) {
// Decide the length of the tail of the arrow here. It is currently 2*n below
for(int arrowLength = 1; arrowLength <= 2*n; arrowLength++) {
System.out.print("*");
}
}
// This is where the tail of the arrow ends
System.out.println(""); //Next Line
}
输入为10:
*
**
***
****
*****
******
*******
********
*********
******************************
*********
********
*******
******
*****
****
***
**
*