I have the following code...
String t = " ";
for(int l=0; l<=5; l++){
t = "Num: " + l + "\n";
}
VarPrueba.setText(t);
I am wanting to loop through a set of numbers, and generate a String
that lists them all at the end. The output should be something like this...
1
2
3
4
5
Could someone please help me understand how to correct my code.
String t = " "; for(int l=0; l<=5; l++){ t = "Num: " + l + "\n"; } VarPrueba. setText(t);
September 2, 2020. A loop is a structure in programming that allows you to run the same section of code over and over. This can be used when you want to perform an iterative task (like counting, or sorting through a list) or to create an ongoing, cyclical experience for the user (as in a game loop).
In Kotlin, the for loop is used to loop through arrays, ranges, and other things that contains a countable number of values.
Change as follow:
t+="Num: " + l + "\n";
And the most effective way to do this is to using StringBuilder
, Something like:
StringBuilder t = new StringBuilder();
for(int l=0; l<=5; l++){
t.append("Num:");
t.append(l+"\n");
}
VarPrueba.setText(t.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With