gpt4 book ai didi

scala - 如何在scala中增加字符

转载 作者:行者123 更新时间:2023-12-04 19:38:24 24 4
gpt4 key购买 nike

我正在编写代码来打印所有字符及其从“a”到“z”的 ascii 值。

使用以下代码片段我可以做到这一点。

var c = 'a'
while(c < 'z'){
println(c +" = " + c.toInt)
var p = c.toInt
p += 1
c = p.toChar
}

但是当我在做以下事情时(比如c)

var c = 'a'
while(c < 'z'){
println(c +" = " + c.toInt)
c += 1 // or c = c + 1.toChar
}

它给我以下错误

 found   : Int
required: Char

有没有更好的方法来增加 scala 中的字符。

谢谢,
山塔努

最佳答案

根据要求,这是从评论中移出的另一个选项:

('a' to 'z').map(_.toInt).foreach(println)

关于scala - 如何在scala中增加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20814164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com