0
Can someone help me with this question. I don't know how to count all inputs. KOTLIN
The given code uses an infinite while loop to take continuous user input. During each iteration a number is taken from the input. You need to fix the program to stop the loop when the user enters 0 and output the number of inputs taken before that. Sample Input: 42 1 66 0 Sample Output: 3
4 Réponses
+ 3
Not exact code but an idea,
count=0
while(true) or while(1) :
a=input;
if(a==0) :
//print the count variable
break
else:
//increment count by 1
+ 3
Use this code but not blindly, try to understand the codes, try on yourself :
fun main(args: Array<String>) {
var count = 0
while(true) {
var input = readLine()!!.toInt()
if(input==0){
println(count)
break
}else{
count+=1
}
}
}
+ 1
//you can try this.
fun main(args: Array<String>){
var num=mutableListOf<Int>()
while(true){
var input=readLine()!!.toInt()
if(input==0){
break
}else{
num.add(input)
}
}
println(num.size)
}