我一直在与 Julia 一起解决哈佛 CS50 中的问题集。这个脚本是我对[复数选举的解决方案。] 1
println("How many contenders do we have?")
const max_candidates = parse(Int, readline()) # a maximal number of candidates
# Let us define a composite type for the candidates in our elections
mutable struct Candidate
name::String
votes::Int64
end
function vote(name)
for i in 1:max_candidates
if candidates[i].name == name
candidates[i].votes = candidates[i].votes + 1
end
end
end
function print_winner()
max_votes = 0
for i in 1:max_candidates
if candidates[i].votes > max_votes
max_votes = candidates[i].votes
end
end
for i in 1:max_candidates
if candidates[i].votes == max_votes
candidates[i].name
end
end
end
candidates = Vector{Candidate}(undef, max_candidates)
for i in 1:max_candidates -1
println("Name of the candidate: ?")
name = readline()
votes = 0
candidates[i] = Candidate(name, votes)
println("Thank you, let us move to the next candidate.")
end
#The last candidate i registered outside of the loop because I do no want
#the line println("Thank you, let us move to the next candidate.") to be executed after them.
println("Name of the last candidate: ?")
name = readline()
votes = 0
candidates[max_candidates] = Candidate(name, votes)
println("How many voters do we have?")
voter_count = parse(Int, readline())
for i in 1:voter_count
println("Who are you voting for?")
name = readline()
vote(name)
end
winner = print_winner()
println(winner)
Run Code Online (Sandbox Code Playgroud)
当我运行此脚本时,出现以下错误
ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.
Stacktrace:
[1] print(::Base.TTY, ::Nothing) at ./show.jl:566
[2] print(::Base.TTY, ::Nothing, ::Char) at ./strings/io.jl:42
[3] println(::Base.TTY, ::Nothing) at ./strings/io.jl:69
[4] println(::Nothing) at ./coreio.jl:4
[5] top-level scope at none:0
[6] include at ./boot.jl:317 [inlined]
[7] include_relative(::Module, ::String) at ./loading.jl:1044
[8] include(::Module, ::String) at ./sysimg.jl:29
[9] exec_options(::Base.JLOptions) at ./client.jl:266
[10] _start() at ./client.jl:425
in expression starting at /home/jerzy/C.../plurality.jl:65
Run Code Online (Sandbox Code Playgroud)
错误消息中称为“从 /home/jerzy/C.../plurality.jl:65 开始的表达式”的表达式是脚本的姓氏。我不明白这是nothing什么?尽管如此,按照错误消息的建议,我修改了代码的最后一行,将其更改为:
println(winner)
Run Code Online (Sandbox Code Playgroud)
到
show(winner)
Run Code Online (Sandbox Code Playgroud)
并得到以下输出:
没有
我在这里和那里做了一些研究,但作为一个新手,我不明白为什么我不能从我的函数print_winner返回一个值。从我读到的,返回语句不是强制性的。
在print_winner的定义中,当我替换
candidates[i].name
Run Code Online (Sandbox Code Playgroud)
和
println(candidates[i].name)
Run Code Online (Sandbox Code Playgroud)
然后当最后一行是
winner = print_winner()
Run Code Online (Sandbox Code Playgroud)
那么我终于可以得到获胜者的名字了。但这不是我想要的方式。我想返回一个值并将其分配给一个变量,然后对这个变量做一些事情。我可以在 PHP 或 Racket 中执行此操作,为什么我不能在 Julia 中执行此操作?
该函数print_winner不返回任何内容,在这种情况下nothing实际返回对象。所以winner得到值nothing(来自winner = print_winner()),并println(winner)等价于println(nothing),从而导致错误。
我想返回一个值并将其分配给一个变量
然后就这样做:从print_winner. Julia 不知道你希望这个函数返回什么,所以你必须明确说明它。默认情况下,Julia 返回函数表达式的值,在本例中是最后一个表达式的结果,这里是一个for循环。for-loop的表达式值nothing在 Julia 中。