Vb.net 使用循环允许我在控制台应用程序中输入几个数字,然后进行计算

Vb.net 使用循环允许我在控制台应用程序中输入几个数字,然后进行计算,vb.net,Vb.net,这基本上就是我任务的细节 使用循环编写visual basic控制台应用程序将允许您输入任意数量的数字。然后应用程序应计算用户输入的所有数字的平均值。应使用循环验证输入,以确保输入的数据为有效数字。如果输入的值无效,则应提示用户输入有效数字 将以下内容输出到控制台 输入的数字总和: 输入的总数: 输入的数字的平均值: 我的想法是: 让用户提示输入一个数字 检查它是否为数字 如果不是数字,则再次提示 如果是数字,则提示输入更多数字 有一个选项,用户可以键入一个键或关键字,程序停止收集数字,并进行

这基本上就是我任务的细节

使用循环编写visual basic控制台应用程序将允许您输入任意数量的数字。然后应用程序应计算用户输入的所有数字的平均值。应使用循环验证输入,以确保输入的数据为有效数字。如果输入的值无效,则应提示用户输入有效数字

将以下内容输出到控制台

输入的数字总和:

输入的总数:

输入的数字的平均值:

我的想法是:

  • 让用户提示输入一个数字
  • 检查它是否为数字
  • 如果不是数字,则再次提示
  • 如果是数字,则提示输入更多数字
  • 有一个选项,用户可以键入一个键或关键字,程序停止收集数字,并进行计算,输出以下三项所需内容
我的问题是,我不能真正想象如何去做,或者如何以这样的方式来写。到目前为止,我学习的第一个问题是无法看到它是如何工作的,或者代码的示例,但是当我看到时,我可以理解它并有效地使用它。我只是难以想象如何做/写它

Dim str_num As String
Dim int_num As Integer
Dim int_counter As Integer

Sub Main()
    Console.BackgroundColor = ConsoleColor.DarkBlue
    Console.ForegroundColor = ConsoleColor.White
    Console.Clear()

    Do
        Console.WriteLine("enter a number") 'prompts user to enter a number, followed by it storing in a variable.
        str_num = Console.ReadLine

        If Not IsNumeric(str_num) Then
            int_num = str_num
            Console.ReadKey()

        End If
    Loop
End Sub

看起来您想自己编写代码,但缺少一些资源。我会给你资源。您将实现逻辑。成交

以下是一种从用户处获取输入并验证其是否为数字的方法:

    Dim userInput As String
    Console.WriteLine("Enter a number:")
    userInput = Console.ReadLine()

    Dim inputAsInteger As Integer = 0
    If Not Int32.TryParse(userInput, inputAsInteger) Then 'TryParse returns a Boolean which reads True if it worked 
        Console.WriteLine("Hey, that wasn't a number!")
    End If
以下是一个循环逻辑,可用于收集输入:

    Dim stopLooping As Boolean = False
    Dim i As Integer = 0 'I'll count the loops with this integer, but the exit condition can be anything

    Do
        'Some code which do your stuff

        'At some point, a condifion can flip stopLooping to True when you want the loop to stop
        Console.WriteLine("Loop number " & i.ToString)
        If i > 10 Then
            stopLooping = True
        End If
    Loop Until stopLooping

现在你应该能够从这些工具中找到一些东西,对吗?我相信你会的。玩得开心

在线评论和解释

Sub Main()
    Dim num As Integer
    'With a list, unlike an array, we don't have to know beforehand how many items we are going to add.
    Dim lst As New List(Of Integer)
    Do
        Console.WriteLine("Enter a whole number.")
        'A nested Do will test for a proper number
        Do
            'Integer.TryParse will return true if a valid number was entered and fill in the 
            'num variable with that number
            If Integer.TryParse(Console.ReadLine, num) Then
                'Since we have a valid number we add it to the lst
                lst.Add(num)
                'and exit the inner Do
                Exit Do
            Else
                'If it isn't valid, we ask again for an input, the inner loop continues until we get a valid input
                Console.WriteLine("Sorry, not a valid number. Please try again")
            End If
        Loop
        Console.WriteLine("Is that enough numbers? Yes, No")
        'Call .ToUpper on the input so we don't have to depend on what case the user typed in
        If Console.ReadLine.ToUpper = "YES" Then
            Exit Do
        End If
    Loop
    'We can use the .Sum method and .Average method of the list to do the calculaion
    Dim sumNum = lst.Sum()
    'I have used Interpolated strings indicated by the $. These allow you to insert a variable 
    'surrounded by braces {} directly into a string.
    'In Visual Studio prior to version 2015
    'use Console.WriteLine(String.Format("The sum of the numbers you entered is {0}", sumNum))
    Console.WriteLine($"The sum of the numbers you entered is {sumNum}")
    Dim avgNum = lst.Average()
    Console.WriteLine($"The average of the numbers your entered is {avgNum}")
    Console.ReadLine()
End Sub

您可以更改您的
Do。。。循环
执行。。。循环直到
,因此您现在有一个停止条件。至于循环内部,可以将有效的数字添加到列表中,而不必处理非数字(字符串)。如果字符串等于关键字,则它将退出。如果没有,则再次提示用户。一旦循环退出,您可以从列表中获得总和、总数和平均值。我可以看一个例子吗?我是一名视觉学习者。如果问题太复杂,请将其分解!例如,您可以编写一个函数,其唯一目的是从控制台读取数据,并尝试对其进行解析。如前所述,如果辅助数组是一个设置大小,您可以将数字存储到一个列表中。看看这个例子:我还是不能。不幸的是,我真的很难处理循环。我上这门课还不到两个月,我们今天才真正开始我们的循环模块。我很沮丧,我不能理解/掌握这个概念。或者我理解循环的一个非常基本的概念,基本上是如何从变量中获取数字并将其增加多少个数字,但除此之外,我真的感到困惑和沮丧。当你一步一步地写下你希望代码做的事情时,你走上了正确的轨道。这基本上是伪代码。许多学生大大低估了伪代码。您可以立即编写“简单”的内容,但任何复杂的内容都值得编写伪代码,即使它不详细。@SilasLeFriend您可以通过设置退出条件(例如,当用户键入“特殊停止词”时)来改进您的循环(您发布的循环)。然后再次尝试你的循环,看看它是否有效。循序渐进的工作是确保一切按你的意愿进行的好方法。是的,这是一个好的开始。有很多地方需要改进,但你会及时得到的。现在,在循环外声明一个变量,以便对用户的输入求和,并实现它。提示:每次
TryParse
说它是一个数字。。。把它加起来。