Write a Scala program to find the larger value from two positive integer values in the range 20..30 inclusive, or return 0 if neither is in that range.
object scala_basic { def test(x: Int, y: Int): Int = { val max_of_two = List(x, y).max if (max_of_two >= 20 && max_of_two <= 30) max_of_two else 0 } def main(args: Array[String]): Unit = { println("Result: " + test(78,95)); println("Result: " + test(20,30)); println("Result: " + test(21,25)); println("Result: " + test(28,28)); } }
Sample Output:
Result: 0 Result: 30 Result: 25 Result: 28
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer