public class Asterisk
{
public static void main(String[] args)
{
String output="";
int count=1, input;
System.out.println("Input the size of the triangle from 1 to 50:");
input = 5;
for(count=1;count <= input;count++)
{
output += "*";
System.out.println(output);
}
input -= 1;
for(count =input;count <= input;count--)
{
output = output.substring(0,count);
System.out.println(output);
}
}
}
Mon code se complique correctement et s'exécute correctement. Cependant au bas de la sortie, il imprime une erreur en disant:substring() dans la boucle provoquant "StringIndexOutOfBoundsException: l'index de chaîne est hors de portée: -1"
Exception dans le thread "principal" java.lang.StringIndexOutOfBoundsException:
index de chaîne hors de portée: -1
at java.lang.String.substring(String.java:1937) at Asterisk.main(Asterisk.java:18)
Quelqu'un peut-il expliquer ce comportement étrange? Merci!
Merci beaucoup! Il suffit d'apprendre Java, donc excusez l'erreur débutant! :) –