-1
효과가 있지만 충분하지 않다고 생각합니다. if-let 구조 외부에서 readLine()을 사용할 수 있습니까 ??? 나는 그 범위를 매우 제한적으로 생각하며,이 방법을 사용하여 커맨드 라인에서 이것을 사용하여 인자를 전달할 수 있다고 믿지 않습니다. 누구든지이 문제에 대한 더 나은 접근법을 제안 할 수 있습니까?이 신속한 readLine 코드를 개선하려면 어떻게해야합니까?
import Foundation
print("\nTemperature Conversion\n")
print("What is your current Temperature Unit?\n ")
print("Valid options are f or F for Fahrenheit, c or C for Celsius: \n")
if var temp = readLine() {
switch temp{
case "c","C":
print("And the temperature: ")
if var degrees = readLine() {
var fahr = (5 * Float(degrees)! * 1.8 + 32)
print("\(degrees) Degrees is equal to \(fahr) degrees Fahrenheit. \n")
} else {
print("You entered an invalid temperature")
}
case "f","F":
print("Fahrenheit")
if var degrees = readLine() {
var celsius = (5 * Float(degrees)! - 32)/9;
print("\(degrees) Degrees is equal to \(celsius) degrees Fahrenheit. \n")
} else {
print("You entered an invalid temperature")
}
default:
print("Not a valid Temperature unit")
}
}