我在制作第一个程序时遇到了这个错误:致命错误:在打开可选值时意外发现nil



我试图深入了解swift,并开始尝试使其变得简单,但我一直收到这样的错误:"致命错误:在展开可选值"时意外发现nil;

我到处看了看,用谷歌搜索,尝试了他们让我做的事情,但都不起作用,我觉得自己很愚蠢。

这是我的代码:

import Foundation
import Glibc
print("How much did you spend today: ")
if let input = Int(readLine()!){
print("You have spent", input)
}
else{
print("The input is not a number")
}

https://www.journaldev.com/19612/swift-readline-swift-print对于新手来说,这是一个很好的入门教程。。。

试试这个:

import Foundation
import Glibc
print("How much did you spend today: ")
if let input = readLine(){
if let intputInt = Int(input) {
print("You have spent", inputInt)
}
else {
print("The input is not a number")
}
}
else{
print("The input is not valid")
}

相关内容

最新更新