2017-10-15 10 views
0

이 프로그램의 목적은 사용자가 삼각형의 길이 (행)를 결정하고 위 또는 아래로 향해야하는지 결정하는 것입니다. 그리고 삼각형 문자로 만든, 그래서 다음과 같이 가정한다 :잘못된 순서로 삼각형 인쇄 문자 및 원하지 않는 문자

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); 

        } 
       } 
      } 
     } 
    } 

} 

답변

0
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) { 
      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 = 0; i < a; i++) { 
        for (int j = 0; j <= i; j++) { 
         System.out.print((char) (j + 'A')); 
        } 
        System.out.println(); 
       } 
      } else { 
       for (int i = 0; i < a; i++) { 
        for (int j = a; j > i; j--) { 
         System.out.print((char) (a - j + 'A')); 
        } 
        System.out.println(); 
       } 
      } 
     } 
    } 
} 
0

은 아래 더 모듈 식 솔루션을 채택 :

이 지금까지 내 코드입니다. 4 개의 행으로 구성되는 삼각형을 모두 인쇄하고, 하나는 위를 향하게하고 다른 하나는 아래로 향하게하여 인쇄합니다.

public static void main(String args[]) { 
    int a = 4; // # of rows 
    // Triangle facing up 
    for (int i = 1; i <= a; i++) // i - How many letters in this row (also row No) 
    printRow(i); 
    System.out.println("--------"); // Separator 
    // Triangle facing down - Start from the longest row, then decrease its length 
    for (int i = a; i > 0; i--) 
    printRow(i); 
} 

static void printRow(int length) { 
    for (int j = 0; j < length; j++) // j - char code shift 
    System.out.printf("%c ", j + 'A'); 
    System.out.println(); 
} 

행을 인쇄하는 코드가 반복되지 않기 때문에이 솔루션은 더욱 우아합니다.

연속적인 행의 길이를 나타내는 더 자연스러운 방법에 유의하십시오. 아래쪽을 향한 삼각형의 경우 행 길이가 줄어 듭니다.