Skip to content

Commit f570a81

Browse files
committed
仓颉试用完
1 parent 1702210 commit f570a81

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

_posts/2024-07-27-仓颉语言试写猜数字(三).md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
https://zhuanlan.zhihu.com/p/711324572
12

23
[上文](https://zhuanlan.zhihu.com/p/710551238)
34

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
https://zhuanlan.zhihu.com/p/711325333
2+
3+
[上文](https://zhuanlan.zhihu.com/p/711324572)
4+
5+
在仓颉编程语言库 API 文档里找线索,搜 string 仅有 stringReader/Writer
6+
7+
再搜 int,看到 BigInt 示例里:`let int = BigInt("-123456")`
8+
9+
于是试试:`let sint = Int("123")`
10+
11+
报错:
12+
```
13+
error: no matching function for operator '()' function call
14+
==> tests/int.cj:5:20:
15+
|
16+
5 | let sint = Int("123")
17+
| ^
18+
```
19+
20+
如果用 Int64,还是见过的错误:
21+
22+
```
23+
error: the expression for numeric type conversion must have a numeric type
24+
==> tests/int.cj:5:26:
25+
|
26+
5 | let sint = Int64("123")
27+
```
28+
29+
如果用 BigInt 比较整数呢?那样可以把字符串转为 BigInt 再比较。
30+
31+
```
32+
error: cannot convert an integer literal to type 'Struct-BigInt'
33+
==> tests/int.cj:8:29:
34+
|
35+
8 | println(int.compare(1))
36+
```
37+
38+
还有啥办法?std库逐个看过去,发现 std.convert !!例程里有:
39+
`println("After the conversion of parse, \"-9223372036854775808\" became ${Int64.parse(strInt64_parse)}")`
40+
41+
马上试:
42+
43+
```
44+
let i = Int64.parse("23")
45+
```
46+
47+
还是报错:
48+
49+
```
50+
error: undeclared identifier 'parse'
51+
==> tests/int.cj:9:23:
52+
|
53+
9 | let i = Int64.parse("23")
54+
| ^^^^^
55+
```
56+
57+
需导入此库:`import std.convert.*`,运行通过。这时发现上一篇试过 `Int64.parse`,但就是未导入库。
58+
59+
回头看,api文档仅提供了在文档标题中搜索的功能。要是可以将报错信息和api文档全文搜索结合起来,也许可以方便用户。[《一站式 IDE》](https://zhuanlan.zhihu.com/p/260117393) 提到过:
60+
61+
> 在开发全过程中,对各种报错警告信息都可以指向相关的知识点、例程、或是他人提过的问题和解决方法(如何集成论坛或是问题追溯平台功能另行讨论)。
62+
63+
完成:
64+
65+
```
66+
import std.console.*
67+
import std.random.*
68+
import std.math.*
69+
import std.convert.*
70+
71+
main() {
72+
let 想的 = abs(Random().nextInt64()%50)
73+
74+
while(true) {
75+
let 猜的 = Int64.parse(Console.stdIn.readln() ?? "")
76+
if (猜的 > 想的) {
77+
println("大了")
78+
} else if (猜的 < 想的) {
79+
println("小了")
80+
} else {
81+
println("${猜的} 中了!")
82+
break
83+
}
84+
}
85+
}
86+
```

assets/2024-07-05-仓颉异常.png

445 KB
Loading

0 commit comments

Comments
 (0)