三角形打印字母顺序错误,不需要的字符

时间:2017-10-15 13:44:29

标签: java for-loop while-loop char

此程序的目的是让用户决定三角形的长度(行),并确定它应该朝上还是朝下。三角形是由字母组成的,因此应该看起来像这样:

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

                    }
                }
            }
        }
    }

}

2 个答案:

答案 0 :(得分: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();
                }
            }
        }
    }
}

答案 1 :(得分: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();
}

此解决方案更优雅,因为不会重复打印行的代码。

还要注意表达连续行长度的更自然的方法:对于面向下的三角形,循环会减小行长度。