ballerina - 如何从 Ballerina 的命令行中读取 int?
问题描述
any choice = io:readln("Enter choice 1 - 5: ");
我似乎无法将输入转换为 int。
检查和匹配都给出相同的错误
var intChoice = <int>choice;
match intChoice {
int value => c = value;
error err => io:println("error: " + err.message);
}
和
c = check <int>choice;
给
error: 'string' cannot be cast to 'int'
我查看了https://ballerina.io/learn/by-example/type-conversion.html并研究了https://ballerina.io/learn/api-docs/ballerina/io.html#readln但没有运气。
我究竟做错了什么?
解决方案
似乎这是any -> int
转换中的错误。
如果使用 将选择变量类型更改为string
或将变量定义语句更改为赋值语句var
,则两种方法都可以使用。请参考以下示例。
import ballerina/io;
function main(string... args) {
// Change the any to string or var here.
string choice = io:readln("Enter choice 1 - 5: ");
int c = check <int>choice;
io:println(c);
var intChoice = <int>choice;
match intChoice {
int value => io:println(value);
error err => io:println("error: " + err.message);
}
}
更新- 正如@supun 在下面提到的,这不是any->int
转换中的错误,而是我不知道的实现细节。
推荐阅读
- maven - 不使用 settings.xml 中提供的镜像
- oracle - 通过单击 Oracle 表单上的按钮并下载输出来调用 XML 报告
- c# - 如何从像axb这样的文本中获取a和b值?
- node.js - 乘客未启动节点应用程序
- azure - 在文件名中使用通配符 (*) 时,无法从 Azure 数据工厂中的 SFTP 复制文件
- jsp - 如何删除 index.jsp 在 URL 中的显示?
- javascript - Javascript更改svg g路径元素的背景颜色
- excel - VBA中的for循环
- sockets - 如何在一个套接字 C++ 中发送和接收多个文件大小?
- grep - 如何每行grep多个模式