Java在同一行上写入三角形
我是编程新手,但我的家庭作业需要一些帮助。我试图创建4个三角形使用星号,我已经做了,一个在另一个下面,但我需要他们在同一行,这是令人困惑的部分 应该是这样的Java在同一行上写入三角形,java,Java,我是编程新手,但我的家庭作业需要一些帮助。我试图创建4个三角形使用星号,我已经做了,一个在另一个下面,但我需要他们在同一行,这是令人困惑的部分 应该是这样的 * ******** ******** ** ******* ******* *** ****** ****** **** ***** ***** ***** **** **** ****** *** *** ***
* ******** ********
** ******* *******
*** ****** ******
**** ***** *****
***** **** ****
****** *** ***
******* * *
这就是我目前得到的
public class Practice {
public static void main(String[] args) {
for (int row = 0; row < 11; row++) {
for (int a = 0; a < row; a++)
System.out.print("*");
for (int star = 0; star < 2; star++)
System.out.print("\t");
for (int b = 10; b > row; b--)
System.out.print("*");
System.out.println();
}
}// end main method
}// end class Triangle
试试这个
int max = 11;
for (int row = 1; row <= max; row++) {
for (int a = 0; a <row; a++){
System.out.print("*");
}
for (int star = max; star >=row; --star){
System.out.print(" ");
}
for (int b = max; b >= row; b--){
System.out.print("*");
}
System.out.println();
}
int max=11;
对于(int row=1;row=row;b--){
系统输出打印(“*”);
}
System.out.println();
}
尝试此选项以获得输出
public class TriangleOnRow {
public static void main(String[] args){
int max = 11;
for (int row = 1; row <= max; row++) {
for (int a = 0; a <row; a++){
System.out.print("*");
}
for (int c = max; c >=row; c--){
System.out.print(" ");//2 whitespace's
}
for (int b = max; b >= row; b--){
System.out.print("*");
}
for (int star = 0; star <=row; star++){
System.out.print(" ");//1 whitespace
}
for (int star = row; star >=0; --star){
System.out.print(" ");//3 whitespace's
}
for (int c = max; c >=row; c--){
System.out.print("*");
}
System.out.println();
}
}
}
公共类行{
公共静态void main(字符串[]args){
int max=11;
对于(int row=1;row=row;c--){
系统输出打印(“*”);
}
System.out.println();
}
}
}
您应该多尝试一下。它将帮助您理解循环的概念。
public class TriangleOnRow {
public static void main(String[] args){
int max = 11;
for (int row = 1; row <= max; row++) {
for (int a = 0; a <row; a++){
System.out.print("*");
}
for (int c = max; c >=row; c--){
System.out.print(" ");//2 whitespace's
}
for (int b = max; b >= row; b--){
System.out.print("*");
}
for (int star = 0; star <=row; star++){
System.out.print(" ");//1 whitespace
}
for (int star = row; star >=0; --star){
System.out.print(" ");//3 whitespace's
}
for (int c = max; c >=row; c--){
System.out.print("*");
}
System.out.println();
}
}
}