Hello, I’m a one of beginner and I try to build if I put in a number, prints minimum number until I put in negative number.
And it should be prints like “The current number is xxx”
But I have a no idea what’s problem
You can use 3 backticks to open a new code block and 3 backticks again to close it (they need to be on an empty line. I added them inside the code).
// ``` open codeblock
fun main(){
var currentMin = {n : Int ->
var initNum = n
if(initNum > n){
initNum = n
print(initNum)
}
else{
initNum = initNum
print(initNum)
}
}
while(true){
print("\nEnter a number : ")
var x = readLine()!!.toInt()
if (x>=0) {
if (x>=100){
println("Cannot put in a Number Over 100!!")
}
else {
print_result(currentMin(x))
}
}
else {
println("Exit Program")
break
}
}
}
fun print_result(function: Unit){
function("The Current Num is ") // error: type Unit is not a function
}
// ``` close codeblock
Obviously I could give you a working example, but maybe we can try to fix yours.
Maybe try to explain what you think your code is actually doing / supposed to do.
Here are a few questions that might help you:
You say you always want to print the lowest number. So if you input first a 3 and than a 5 it should print “The current number is 3” twice? Or should it print 3 and than 5?
if it should print 3 twice, where do you save the lowest number. What type has the variable currentMin? Does it actually do what you expect?
if it should print 3 and than 5, what does the currentMin part do all? Why not use println instead.
What do you want the function print_result to do? Obviously there is a compiler error right now, but what should it do? Can you maybe replace it with println directly and just pass it the lowest value? This is again related to the type of currentMin.
How did you come up with this code. Obviously you can make it work, but I think it is overly complicated. Why not just save the currentMin in a variable and move all the the other code into the while loop?