我正在尝试创建一个程序,它接受三个数字的输入,平均它们,并打印平均值。 我到目前为止的代码:
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
我输入三个数字,然后我的程序显示文字“你的平均值是”但那里没有价值;在文本之后它只是空白。
答案 0 :(得分:3)
尝试使用字符串连接
Console.WriteLine("Your average is: " + Average.ToString)
或使用复合格式字符串
Console.WriteLine("Your average is: {0}", Average)
See MSDN on Console.WriteLine(string, object[])
编辑:在尝试执行添加和分割之前,您需要将该字符串输入转换为正确的数字
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
。这种自动转换是邪恶的。
答案 1 :(得分:1)
我想是这样的:
Console.WriteLine("Your average is: ", Average)
你实际上是这个意思:
Console.WriteLine(String.Format("Your average is: {0}", Average))
答案 2 :(得分:-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()