0
"While" recursion not working
Digital Sum
11 odpowiedzi
0
I will have a look.Thank you very much.
+ 3
This is a loop, not recursion.
You need to put your println statement in a new line (add a line break after the braces). In Kotlin the end of line has significance for the syntax.
And you probably want to print the sum variable, because num will always end up being zero.
+ 1
That depends on the specific code. I prefer functional coding style which involves a lot of method chaining, and programming with expressions rather than statements. in this case, to make the code shorter, sometimes I can combine several things in a single line. but if I have a complex method chain, I prefer to put each step in a new line, just to make it easier to read.
If you code in imperative style and you use statements, you can only put multiple statements in the same line if you use semicolon between them.
But really the point of clean Kotlin code is that you can avoid semicolons altogether.
And your code is also much more readable , if it is nicely structured in separate lines.
+ 1
I tend to be quite fussy about how I arrange and format my code, so I encourage you to check some of my Kotlin codes and see for yourself how I style them. Maybe you'll get some ideas :)
0
fun main(args: Array<String>) {
var num = readLine()!!.toInt()
var sum: Int =0
while (num>0){
sum+=num%10;
num=num/10;
}println(num)
}
0
Wow.it worked thank you.
Loop*Sorry bout that
0
Quick qn.So in Kotlin...it's safer putting code in different lines.?
0
Mucha Gracias 👊🤝
0
int i = 5;
while(i--){
recursiveFunction();
}
0
I can't actually tell
0
Опоаовоаьаочооаоаоаосокловов