I am extremely new to coding and learning it in my school currently. My teacher assigned us to write a method that would take a string and return it looking as a triangle, for example if dog was entered it would return:
ddd oo g all on separate lines.
What I have so far is below, I keep getting an error when trying to use substring in the method saying the string type isn't applicable here. Can someone help me?
String go( String a)
{
int count = 0;
for(int i = a.length(); i<=0; i--)
{
While(a.substring(count<=i));
{
System.out.println(a.substring(count,count+1));
count++;
}
}
}
The keyword is while
, not While
.
It needs a boolean condition, while a.substring(…)
looks as if you want to pass a string instead. That's probably not your intent: You already have a boolean condition there: count <= i
, which should be the sole argument for the while
loop.
This is how you wannna write your while loop:
while(count <= i){
System.out.println(a.substring(count,count+1));
count++;
}
It's while
and not While
. I suggest another solution with two for
loops.
public String go(String a) {
int l=a.length();
String ch="";
for (int i=0;i<l;i++) {
for (int j=0;j<l-i;j++)
ch = ch + a.charAt(i);
}
return ch;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.