简体   繁体   English

如何正确打印此三角形的字符?

[英]How do I correctly print this triangle of characters?

This code asks the user for a triangle height and a letter. 此代码要求用户提供三角形高度和字母。

import java.util.*;
public class triangle
{
    public static void main(String [] args)
    {
        Scanner kb = new Scanner(System.in);
        int size = 0;
        Character c;

        System.out.println("Enter height of the triangle : ");
        size = kb.nextInt();

        System.out.println("Which character you want to use : ");
        c = kb.next().charAt(0);

        int i, j, k;

        for (i = 0; i < size + 1; i++)
        {
            for (j = size; j > i; j--) 
            {
                System.out.print(" ");
            }
            for (k = 0; k < (2 * i - 1); k++) 
            {
                System.out.print(c);
            }
            System.out.println();
        }
    }
}

For a height of 3 and the letter b , current output is: 对于高度3和字母b ,电流输出为:

  b
 bbb
bbbbb

But it should be: 但是应该是:

  b
 cbc
dcbcd

How can I print letters in the alphabet after the given letter in the triangle pattern shown above? 如何在上述三角形图案中给定字母之后打印字母中的字母?

Your problem 你的问题

Let's talk about your variable c , which is a char . 让我们谈谈您的变量c ,它是一个char (Actually, c is a Character , the boxed equivalent of a char , but that doesn't matter too much.) (实际上, c是一个Character ,等效于char的盒装形式,但这并不重要。)

In Java, char is a number type . 在Java中, char是数字类型 All char s are numbers. 所有char都是数字。 For example, the char literal 'a' is actually the number 97: 例如, char常量'a'实际上是数字97:

'a' == 97; // true

And you can do arithmetic with char , just as you can with regular numbers: 您可以使用char进行算术,就像使用常规数字一样:

'a' + 1; // 98
'a' + 2; // 99
'a' + 3; // 100

The mapping between characters like 'a' and numbers like 97 in Java follows the universal standard Unicode , a superset of the older standard ASCII . Java中字符'a'和数字“ 97 'a'之间的映射遵循通用标准Unicode ,这是旧标准ASCII的超集。 In other words, 'a' == 97 because ASCII says so. 换句话说, 'a' == 97因为ASCII表示。 Here's an ASCII table: 这是一个ASCII表:

Both ASCII and Java's char were thoughtfully designed so that doing arithmetic with characters works exactly like you'd expect. ASCII和Java的char都是经过精心设计的,因此对字符进行算术的工作与您期望的完全一样。 Study these examples: 研究以下示例:

(char) ('a' + 1); // 'b'
(char) ('a' + 2); // 'c'
(char) ('a' + 3); // 'd'

As you see, in the last line, adding 3 to 'a' and then converting it back to a char produces 'd' , which is 3 letters after 'a' . 如您所见,在最后一行中,将3加到'a' ,然后将其转换回char产生'd' ,它在'a'之后3个字母。

So if you want x letters after some character c , the code is just (char) (c + x) . 因此,如果您想在某个字符c后面加上x个字母,则代码就是(char) (c + x)

In your case, for each letter you print in the triangle, you want to increase the user's character (in your example, 'b' ) by the letter-to-print's distance from the central letter. 在您的情况下,对于您在三角形中打印的每个字母,您都希望通过字母到打印字符到中心字母的距离来增加用户的字符(在您的示例中为'b' )。 The letter-to-print is in position k , and central letter is always in position i - 1 ; 待打印字母位于位置k ,中央字母始终位于位置i - 1 thus the distance is Math.abs(k - (i - 1)) , or Math.abs(k - i + 1) . 因此,距离是Math.abs(k - (i - 1))Math.abs(k - i + 1) That's how much to add to the character. 那就是要增加多少字符。

With this knowledge, it's simple to fix your code. 有了这些知识,很容易修复您的代码。 Replace: 更换:

for (k = 0; k < (2 * i - 1); k++) 
{
    System.out.print(c);
}

with: 有:

for (k = 0; k < (2 * i - 1); k++) 
{
    System.out.print((char) (c + Math.abs(k - i + 1)));
}

Other problems 其他问题

While you're here, you may as well fix some other issues with your code: 在这里时,您还可以修复代码中的其他一些问题:

  1. In Java, it's convention for classes to begin with a capital letter. 在Java中,类的约定以大写字母开头。 This means you should change: 这意味着您应该更改:

     public class triangle { 

    to: 至:

     public class Triangle { 

    and rename triangle.java to Triangle.java . 并重命名triangle.javaTriangle.java

  2. You don't need to declare variables at the top. 您无需在顶部声明变量。 It's better to declare them when you first need them, allowing you to combine the declaration and initial assignment. 最好在初次需要它们时声明它们,以便将声明和初始赋值结合起来。 For instance, instead of: 例如,代替:

     int size = 0; // ... size = kb.nextInt(); 

    You can simply write: 您可以简单地写:

     int size = kb.nextInt(); 

    Here's your code, refactored to fix this: 这是您的代码,经过重构可以解决此问题:

     Scanner kb = new Scanner(System.in); System.out.println("Enter height of the triangle : "); int size = kb.nextInt(); System.out.println("Which character you want to use : "); Character c = kb.next().charAt(0); for (int i = 0; i < size + 1; i++) { for (int j = size; j > i; j--) { System.out.print(" "); } for (int k = 0; k < (2 * i - 1); k++) { System.out.print((char) (c + Math.abs(k - i + 1))); } System.out.println(); } 
  3. You should use char , not Character , for c . 您应该将char而不是Character用作c Character should only be used if you need to deal with boxing and unboxing (eg you want to define an ArrayList<Character> ), which has nothing to do with your current problem. Character ,如果你需要处理,才应使用装箱和拆箱 (例如,你想定义ArrayList<Character> ),这无关你当前的问题。 So replace: 因此,请替换:

     Character c = kb.next().charAt(0); 

    with: 有:

     char c = kb.next().charAt(0); 
  4. Use System.out.print() for prompts. 使用System.out.print()获得提示。 It allows the user to type the answer on the same line. 它允许用户在同一行上键入答案。 Instead of: 代替:

     System.out.println("Enter height of the triangle : "); int size = kb.nextInt(); System.out.println("Which character you want to use : "); char c = kb.next().charAt(0); 

    Use: 采用:

     System.out.print("Enter height of the triangle: "); int size = kb.nextInt(); System.out.print("Enter character you want to use: "); char c = kb.next().charAt(0); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM