如何正确打印此三角形的字符? [英] How do I correctly print this triangle of characters?

查看:94
本文介绍了如何正确打印此三角形的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码要求用户提供三角形高度和字母.

  import java.util.*;公共课三角形{公共静态void main(String [] args){扫描仪kb =新的扫描仪(System.in);int size = 0;字符c;System.out.println(输入三角形的高度:");大小= kb.nextInt();System.out.println(您要使用哪个字符:");c = kb.next().charAt(0);整数i,j,k;对于(i = 0; i< size +1; i ++){对于(j =大小; j> i; j--){System.out.print(");}对于(k = 0; k<(2 * i-1); k ++){System.out.print(c);}System.out.println();}}} 

对于高度3和字母 b ,当前输出为:

  bbbbbbbbb 

但是应该是:

  bcbcdcbcd 

如何在上面显示的三角形图案中的给定字母之后的字母之后打印字母?

解决方案

您的问题

让我们谈谈变量 c ,它是一个 char .(实际上, c

ASCII和Java的 char 均经过精心设计,因此对字符进行算术的工作与您期望的完全相同.研究以下示例:

 (char)('a'+ 1);//'b'(字符)('a'+ 2);//'C'(字符)('a'+ 3);//'d' 

如您所见,在最后一行中,将3加到'a'上,然后将其转换回 char ,产生'd',在'a'之后的3个字母.

因此,如果您想在某个字符 c 之后加上x个字母,则代码就是(char)(c + x).

在这种情况下,对于您在三角形中打印的每个字母,您都希望增加用户的字符(在本例中为'b'),即字母到打印件距中心的距离信件.待打印字母位于位置 k ,中心字母始终位于位置 i-1 ;因此,距离为 Math.abs(k-(i-1)) Math.abs(k-i + 1).那就是要增加多少字符.

有了这些知识,很容易修复您的代码.替换:

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

具有:

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

其他问题

在这里的同时,您也可以修复代码中的其他一些问题:

  1. 在Java中,类的约定以大写字母开头.这意味着您应该更改:

     公共类三角形{ 

    收件人:

     公共类Triangle{ 

    ,并将 triangle.java 重命名为 Triangle.java .

  2. 您不需要在顶部声明变量.最好在初次需要它们时声明它们,以便将声明和初始赋值结合起来.例如,代替:

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

    您可以简单地写:

      int size = kb.nextInt(); 

    这是您的代码,经过重构可以解决此问题:

      Scanner kb = new Scanner(System.in);System.out.println(输入三角形的高度:");int大小= kb.nextInt();System.out.println(您要使用哪个字符:");字符c = kb.next().charAt(0);对于(int i = 0; i< size +1; i ++){对于(int j = size; j> i; j--){System.out.print(");}对于(int k = 0; k<(2 * i-1); k ++){System.out.print((char)(c + Math.abs(k-i + 1)));;}System.out.println();} 

  3. 对于 c ,您应该使用 char ,而不是 Character . Character 仅在需要处理 字符c = kb.next().charAt(0);

    具有:

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

  4. 使用 System.out.print()进行提示.它允许用户在同一行上键入答案.代替:

      System.out.println(输入三角形的高度:");int大小= kb.nextInt();System.out.println(您要使用哪个字符:");char c = kb.next().charAt(0); 

    使用:

      System.out.print(输入三角形的高度:");int大小= kb.nextInt();System.out.print(输入要使用的字符:");char c = kb.next().charAt(0); 

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:

  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. (Actually, c is a Character, the boxed equivalent of a char, but that doesn't matter too much.)

In Java, char is a number type. All chars are numbers. For example, the char literal 'a' is actually the number 97:

'a' == 97; // true

And you can do arithmetic with char, just as you can with regular numbers:

'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. In other words, 'a' == 97 because ASCII says so. Here's an ASCII table:

Both ASCII and Java's char were thoughtfully designed so that doing arithmetic with characters works exactly like you'd expect. 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'.

So if you want x letters after some character c, the code is just (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. The letter-to-print is in position k, and central letter is always in position i - 1; thus the distance is Math.abs(k - (i - 1)), or 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. This means you should change:

    public class triangle
    {
    

    to:

    public class Triangle
    {
    

    and rename triangle.java to Triangle.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. Character should only be used if you need to deal with boxing and unboxing (e.g. you want to define an ArrayList<Character>), which has nothing to do with your current problem. So replace:

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

    with:

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

  4. Use System.out.print() for prompts. 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);
    

这篇关于如何正确打印此三角形的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆