이 프로그램의 목적은 사용자가 삼각형의 길이 (행)를 결정하고 위 또는 아래로 향해야하는지 결정하는 것입니다. 그리고 삼각형 문자로 만든, 그래서 다음과 같이 가정한다 :잘못된 순서로 삼각형 인쇄 문자 및 원하지 않는 문자
How many rows would you like? (finish with -1): 4
Do you want the triangle to face up (1) or down (2)? 1
A
A B
A B C
A B C D
How many rows would you like? (finish with -1): 6
Do you want the triangle to face up (1) or down (2)? 2
A B C D E F
A B C D E
A B C D
A B C
A B
A
나는 아래로 향하게하여 인쇄 할 수있는 삼각형을 얻을하려고 할 때 먼저 문자는 다음과 같이 두 가지 문제가있다 (이것은 시작해야 A와 함께)
F E D C B A
F E D C B
F E D C
F E D
F E
F
글자 뒤에는 원하지 않는 다양한 문자가옵니다. 나는 많은 것을 시도했지만 아무것도 작동하지 않는 것 같습니다. 나는 정말로 조언을 사용할 수있다.
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int a = 0;
int b = 0;
while (a != -1) {
System.out.println("How many rows would you like? (finish with -1):");
a = scan.nextInt();
if (a != -1) {
b = a - 1;
int j = 'A';
char alphabet = (char) (j + 'A');
System.out.println("Do you want the triangle to face up (1) or down (2)?");
int c = scan.nextInt();
if (c == 1) {
for (int i = 1; i <= b + 'A'; i++) {
for (j = 'A'; j <= i; j++)
System.out.print((char) j + " ");
System.out.println(alphabet);
}
} else {
for (int i = 1; i <= b + 'A'; i++) {
for (j = b + 'A'; j >= i; j--)
System.out.print((char) j + " ");
System.out.println(alphabet);
}
}
}
}
}
}