在字符串金字塔的java计数器 [英] java counter in string pyramid

查看:122
本文介绍了在字符串金字塔的java计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个字符串创建一个倒金字塔。循环从每一端减去一个字符。只有它的右边被减去。我怎样才能把它做到左侧?我怎样才能让它打印正确的每一行的长度?

  int x = 0; 

int space ='';

space = space +'';

int counter = fullName.length(); (x = 0; x {

System.out.println(counter - x + [+ fullName.substring(x,fullName.length() - x)+]);

$ b $ / code $ <$ $ p

解决方案

String#substring 不会替代 String 变量的内部数据。记住一个 String 实例是不可变的。



你应该打印一个打印 fullName 以添加空格:

 <$ c (x = 0; x  System.out.println(spaces + fullName.substring(x, fullName.length() -  X)); 
spaces = spaces +;
}


I'm trying to create an inverted pyramid with a string. The loop subtracts a character from each end. Only the right side of it is being subtracted. How do I get it to do the same for the left side? How can I also get it to print the length of each line correctly?

     int x = 0;

     int space = ' ';

     space = space + ' ';  

     int counter = fullName.length();

     for( x = 0; x < fullName.length()/2; x++ )
     {

         System.out.println( counter - x + " [" + fullName.substring( x, fullName.length() - x ) + "]" );

     }  

解决方案

String#substring does not replace the inner data of the String variable. Remember that a String instance is immutable.

You should print a String spaces variable before printing the fullName in order to add whitespaces:

String spaces = ""
for( x = 0; x < fullName.length()/2; x++ ) {
    System.out.println(spaces + fullName.substring(x, fullName.length()-x));
    spaces = spaces + " ";
}

这篇关于在字符串金字塔的java计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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