Com os conhecimentos aplicados anteriormente, crie uma calculadora que dado dois valores e um número que corresponde a uma operação (constante), retorne o resultado da operação
Last active
March 14, 2022 20:30
-
-
Save jailsonsf/fd7ce3bb737617fdb7456c91285ccd71 to your computer and use it in GitHub Desktop.
Exercício final introdução ao kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner | |
const val SUM = 1 | |
const val SUB = 2 | |
const val DIV = 3 | |
const val TIMES = 4 | |
fun main(args: Array<String>){ | |
val input = Scanner(System.`in`) | |
print("===========") | |
print("Calculator") | |
print("===========\n") | |
println("Opções:\n[1] Soma\n[2] Subtração\n[3] Divisão\n[4] Multiplicação") | |
println("Escolha uma opção: ") | |
val op = input.nextInt() | |
println("Agora digite dois valores: ") | |
val a = input.nextDouble() | |
val b = input.nextDouble() | |
println(calculator(a, b, op)) | |
} | |
fun calculator(x:Double, y:Double, op:Int):Double { | |
when (op) { | |
SUM -> { | |
return x + y | |
} | |
SUB -> { | |
return x - y | |
} | |
DIV -> { | |
return x / y | |
} | |
TIMES -> { | |
return x * y | |
} | |
else -> { | |
println("Opção não encontrada!") | |
return 0.0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment