英文:
how can I print these triangles side by side?
问题
public class MyClass {
public static void main(String[] args) {
for (int r = 1; r <= 10; r++) {
// Draw the first triangle
for (int c = 1; c <= r; c++)
System.out.print("*");
for (int c = r + 1; c <= 10; c++)
System.out.print(" ");
System.out.print(" ");
// Draw the second triangle
for (int c = 1; c <= (11 - r); c++)
System.out.print("*");
for (int c = (11 - r) + 1; c <= 10; c++)
System.out.print(" ");
System.out.print(" ");
// Draw the third triangle
for (int c = 1; c <= (r - 1); c++)
System.out.print(" ");
for (int c = r; c <= 10; c++)
System.out.print("*");
System.out.print(" ");
// Draw the fourth triangle
for (int c = 1; c <= (10 - r); c++)
System.out.print(" ");
for (int c = 10 - r + 1; c <= 10; c++)
System.out.print("*");
System.out.println();
}
}
}
英文:
I'm given the code below that outputs two triangles like this. However I need to modify the code provided to output 4 triangles side by side to look like this I've messed around with it but get confused with all the nested loops? Any help would be awesome.
public class MyClass {
public static void main(String[] args) {
//for (int i=1; i <= 10; i++)
for (int r=1; r<= 10; r++) {
// Draw the triangle 1
//for (int j =1; j <=i; j++)
for (int c =1; c <=r; c++)
System.out.print("*");
for (int c=r+1; c<=10; c++)
System.out.print(" ");
System.out.print(" ");
// Draw the trisngle 2
for (int c = 1; c <= (11 - r); c++)
System.out.print("*");
for (int c = (11 - r) + 1; c <= 10; c++)
System.out.print(" ");
System.out.print(" ");
System.out.println();
} //end of for r
} //end of static void main
}
答案1
得分: 2
在Java 11+中,您可以将其简化为以下形式,使用动态数量的三角形和动态三角形大小。
static void printTriangles(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
if (j % 2 == 0)
System.out.print("*".repeat(i + 1) + " ".repeat(size - i));
else
System.out.print("*".repeat(size - i) + " ".repeat(i + 1));
}
System.out.println();
}
}
Tests
printTriangles(2, 10);
printTriangles(4, 10);
printTriangles(7, 6);
Outputs
* **********
** *********
*** ********
**** *******
***** ******
****** *****
******* ****
******** ***
********* **
********** *
* ********** * **********
** ********* ** *********
*** ******** *** ********
**** ******* **** *******
***** ****** ***** ******
****** ***** ****** *****
******* **** ******* ****
******** *** ******** ***
********* ** ********* **
********** * ********** *
* ****** * ****** * ****** *
** ***** ** ***** ** ***** **
*** **** *** **** *** **** ***
**** *** **** *** **** *** ****
***** ** ***** ** ***** ** *****
****** * ****** * ****** * ******
英文:
In Java 11+, you can make it as simple as this, with dynamic number of triangles, and dynamic triangle size.
static void printTriangles(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
if (j % 2 == 0)
System.out.print("*".repeat(i + 1) + " ".repeat(size - i));
else
System.out.print("*".repeat(size - i) + " ".repeat(i + 1));
}
System.out.println();
}
}
Tests
printTriangles(2, 10);
printTriangles(4, 10);
printTriangles(7, 6);
Outputs
* **********
** *********
*** ********
**** *******
***** ******
****** *****
******* ****
******** ***
********* **
********** *
* ********** * **********
** ********* ** *********
*** ******** *** ********
**** ******* **** *******
***** ****** ***** ******
****** ***** ****** *****
******* **** ******* ****
******** *** ******** ***
********* ** ********* **
********** * ********** *
* ****** * ****** * ****** *
** ***** ** ***** ** ***** **
*** **** *** **** *** **** ***
**** *** **** *** **** *** ****
***** ** ***** ** ***** ** *****
****** * ****** * ****** * ******
答案2
得分: 2
一个对 @Andreas的回答
的跟进,通过添加两种类型/线型来提供所需的三角形类型:
static String pattern(String left, int leftCount, String right, int rightCount) {
return left.repeat(leftCount) + right.repeat(rightCount);
}
static void printTriangles(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
String line = "";
switch(j % 4) {
case 0: line = pattern("*", i + 1, " ", size - i);
break;
case 1: line = pattern("*", size - i, " ", i + 1);
break;
case 2: line = pattern(" ", i + 1, "*", size - i);
break;
case 3: line = pattern(" ", size - i, "*", i + 1);
break;
};
System.out.print(line);
}
System.out.println();
}
}
对于 Java 13+,可以使用更短的 switch
:
static void printTrianglesJava13(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
String line = switch(j % 4) {
case 0 -> pattern("*", i + 1, " ", size - i);
case 1 -> pattern("*", size - i, " ", i + 1);
case 2 -> pattern(" ", i + 1, "*", size - i);
case 3 -> pattern(" ", size - i, "*", i + 1);
default -> "";
};
System.out.print(line);
}
System.out.println();
}
}
printTriangles(4, 10)
的输出结果:
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
英文:
A followup for @Andreas answer
to provide required types of triangles by adding two more types/line patterns:
static String pattern(String left, int leftCount, String right, int rightCount) {
return left.repeat(leftCount) + right.repeat(rightCount);
}
static void printTriangles(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
String line = "";
switch(j % 4) {
case 0: line = pattern("*", i + 1, " ", size - i);
break;
case 1: line = pattern("*", size - i, " ", i + 1);
break;
case 2: line = pattern(" ", i + 1, "*", size - i);
break;
case 3: line = pattern(" ", size - i, "*", i + 1);
break;
};
System.out.print(line);
}
System.out.println();
}
}
For Java 13+ a shorter switch
may be used:
static void printTrianglesJava13(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
String line = switch(j % 4) {
case 0 -> pattern("*", i + 1, " ", size - i);
case 1 -> pattern("*", size - i, " ", i + 1);
case 2 -> pattern(" ", i + 1, "*", size - i);
case 3 -> pattern(" ", size - i, "*", i + 1);
default -> "";
};
System.out.print(line);
}
System.out.println();
}
}
Output of printTriangles(4, 10)
:
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
答案3
得分: 0
我在这里使用了两个计数器,在以下代码中:
public class Temp {
public static void main(String[] args) {
int countA = 1;
int countB = 10;
for (int r = 1; r <= 10; r++) {
for (int i = 1; i <= 10; i++) {
if (countA >= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print(" ");
for (int i = 1; i <= 10; i++) {
if (countB >= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print(" ");
for (int i = 1; i <= 10; i++) {
if (countA > i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.print(" ");
for (int i = 1; i <= 10; i++) {
if (countB > i) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
countB--;
countA++;
System.out.println();
}
}
}
英文:
I have used two counters here, in the following code
public class Temp {
public static void main(String[] args) {
int countA = 1;
int countB = 10;
for (int r=1; r<= 10; r++) {
for (int i=1; i<= 10; i++) {
if(countA >= i) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.print(" ");
for (int i=1; i<= 10; i++) {
if(countB >= i) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.print(" ");
for (int i=1; i<= 10; i++) {
if(countA > i) {
System.out.print(" ");
}else {
System.out.print("*");
}
}
System.out.print(" ");
for (int i=1; i<= 10; i++) {
if(countB > i) {
System.out.print(" ");
}else {
System.out.print("*");
}
}
countB--;
countA++;
System.out.println();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论