vb.net 程序的WriteLine不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15233683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:38:00  来源:igfitidea点击:

WriteLine of program not displaying

vb.netconsoleaverage

提问by Matt

I am trying to create a program that takes the input of three numbers, averages them, and prints the average. The code I have so far:

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

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

I enter three numbers, and then my program displays the text, "Your average is" But there's no value there; it's just blank after the text.

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

回答by Steve

Try with string concatenation

尝试使用字符串连接

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

Or using a composite format string

或者使用复合格式字符串

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

See MSDN on Console.WriteLine(string, object[])

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

EDIT:You need to convert that string input in a correct number before attempting to execute an addition and a division on them

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

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

' and so on for the other inputs '

And, please, do a favor to yourself and set Option Strict Onfor your projects. These kind of automatic conversions are evil.

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

回答by Neolisk

I think by this:

我认为:

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

You actually meant this:

你的意思是这样的:

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

回答by Maynard Tismo Umali

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()