表达式不会产生值

时间:2013-12-12 10:37:21

标签: vb.net

“我有一个代码,但出现了错误(在标题中说明)

Module Module1
Dim direction As String = ""
Sub Main()
    Console.WriteLine("As you step into the dugeon you realise you are stuck. At the end of the path it converges into 3 paths.")
    While direction <> "Left" And direction <> "right" And direction <> "Middle"
        *direction = Console.WriteLine("Do you want to go left, right, or straight up?")*

    End While
    Console.ReadLine()
End Sub

End Module

你知道我可以解决它吗?它之间有星星的代码是不起作用的,所以我只需要正确的代码作为答案,然后我就可以为它欢呼。

2 个答案:

答案 0 :(得分:0)

direction = Console.WriteLine(..)没有意义:写!=阅读。

错误是因为WriteLine有一个 void 结果类型,“不是一个值”。 void type表示没有任何值,不能用于表达式(包括赋值)。

代码应该使用WriteLine 显示文本,然后使用ReadLine来返回字符串,以读取一些输入。还要考虑规范化输入(例如,在读取字符串上使用ToLower/Trim)并匹配条件检查中使用的大小写。

答案 1 :(得分:0)

Module Module1
    Dim direction As String = ""

    Sub Main()
        Console.WriteLine("As you step into the dugeon you realise you are stuck. At the end of the path it converges into 3 paths.")
        Do
            Console.WriteLine("Do you want to go left, right, or straight up?")
            direction = Console.ReadLine()
        Loop While direction <> "Left" And direction <> "right" And direction <> "Middle"
    End Sub
End Module
相关问题