1

我正在尝试创建一个程序,该程序接受三个数字的输入,对它们进行平均,然后打印平均值。我到目前为止的代码:

Sub Main()
    Dim Average As Double

    Console.WriteLine("Please input first number:")
    Dim Num1 As String
    Num1 = Console.ReadLine()

    Console.WriteLine("Please input second number:")
    Dim Num2 As String
    Num1 = Console.ReadLine()

    Console.WriteLine("Please input third number:")
    Dim Num3 As String
    Num3 = Console.ReadLine()

    Average = (Num1 + Num2 + Num3) / 3
    Console.WriteLine("Your average is: ", Average)
    Console.WriteLine("Press any key to exit")
    Console.ReadLine()
End Sub

我输入三个数字,然后我的程序显示文本,“你的平均值是” 但是那里没有值;文本后只是空白。

4

3 回答 3

3

尝试使用字符串连接

Console.WriteLine("Your average is: " + Average.ToString)

或使用复合格式字符串

Console.WriteLine("Your average is: {0}", Average)

请参阅 Console.WriteLine(string, object[]) 上的 MSDN

编辑:在尝试对其执行加法和除法之前,您需要将该字符串输入转换为正确的数字

Dim Num1 as Integer
While(Int32.TryParse(Console.ReadLine(), Num1)
     Console.WriteLine("Please enter a integer number")

' and so on for the other inputs '

而且,请帮自己一个忙,Option Strict On为您的项目做好准备。这种自动转换是邪恶的。

于 2013-03-05T20:44:50.840 回答
1

我认为由此:

Console.WriteLine("Your average is: ", Average)

你实际上是这个意思:

Console.WriteLine(String.Format("Your average is: {0}", Average))
于 2013-03-05T20:46:23.317 回答
-1
Console.WriteLine("Please input first number:")
Dim Num1 As String
Num1 = Console.ReadLine()

Console.WriteLine("Please input second number:")
Dim Num2 As String
Num1 = Console.ReadLine() ------ Use Num2 instead of Num1

Console.WriteLine("Please input third number:")
Dim Num3 As String
Num3 = Console.ReadLine()

Average = (Num1 + Num2 + Num3) / 3
Console.WriteLine("Your average is: ", Average) and use concat "&" here.
Console.WriteLine("Press any key to exit")
Console.ReadLine()
于 2014-09-30T03:53:48.410 回答