替换整数为字符串

huangapple go评论77阅读模式
英文:

Replacing integers to string

问题

我正在尝试在这里打印一些字母我对Java还很陌生无法弄清楚在模式中哪里添加这些字母

package prints;

public class Pattern {
  public static void main(String[] args) {

    int rows = 6;
    int[] diff = {0,1,3,5,7,9,11};
    int blank = 0;
    int b = 0;
    int temp;

    for(int i = rows; i >= 1; --i){

        blank = diff[b++];

        if(blank == 0)
            temp = i-1;
        else
            temp = i;

        for(int j = 1; j <= i; j++){

            System.out.print(" " + j + " ");
        }

        for(int s = 0; s < blank; s++){
            System.out.print("   ");
        }

        for(int k = temp; k >= 1; --k){
            System.out.print(" " + k + " ");
        }
        System.out.println();
    }
  }
}
这是代码在切换语言时遇到了问题
英文:

I am trying to print some alphabets here, I am new to java and can't figure out where to add the alphabets in pattern.

package prints;

public class Pattern {
  public static void main(String[] args) {

    int rows = 6;
    int[] diff = {0,1,3,5,7,9,11};
    int blank = 0;
    int b = 0;
    int temp;
    
    for(int i = rows; i &gt;= 1; --i){
        
        blank = diff[b++];

        if(blank == 0)
            temp = i-1;
        else
            temp = i;

        for(int j = 1; j &lt;= i; j++){
            
            System.out.print(&quot; &quot;+ j + &quot; &quot;);
        }
    
        for(int s = 0; s &lt; blank; s++){
            System.out.print(&quot;   &quot;);
        }
        
        for(int k = temp; k &gt;= 1; --k){
            System.out.print(&quot; &quot; + k + &quot; &quot;);
        }
        System.out.println();            
    }
  }
}

this is the code. trouble in switching languages.

答案1

得分: 1

整数是数字,不是字符。因此,它们无法被“转换”。

但是你可以尝试使用字符。例如:

char h = (char) 72;

然后打印结果:

String.valueOf(h);
英文:

Integers are numbers, not characters. Therefore, they cannot be "converted".

You could however try using a char. For example:

char h = (char) 72;

Then print the result:

String.valueOf(h);

huangapple
  • 本文由 发表于 2020年9月11日 08:13:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63839190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定