首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作业指导

作业指导
EN

Stack Overflow用户
提问于 2016-02-25 07:36:18
回答 3查看 47关注 0票数 0

这是我的编程入门课的第三周,我被卡住了。我可以让它运行,我只是不能输入超过1个数字。这是一个vb控制台应用程序。

代码语言:javascript
复制
Module Module1

    Sub Main()
        Dim highestNumber As Integer = -1000000000
        Dim lowestNumber As Integer = 100000000
        Dim userInput As Integer

        Console.WriteLine("Input your numbers and when you are done enter -99.")
        Console.WriteLine("The app will then give the highest and lowest numbers entered.")
        userInput = Console.ReadLine()

        While userInput <> "-99"
            If userInput >= highestNumber Then
                highestNumber = userInput
            ElseIf userInput <= lowestNumber Then
                lowestNumber = userInput
            End If
        End While

        Console.WriteLine("The highest number you entered is: " & highestNumber)
        Console.WriteLine("The lowest number you entered is: " & lowestNumber)

        Console.ReadLine()

    End Sub

End Module
EN

回答 3

Stack Overflow用户

发布于 2016-02-25 07:49:26

在循环开始之前,您只执行一次ReadLine,所以您只能输入一个数字。按照目前的情况,如果不输入-99,那么就会有一个无限循环,因为您不能在循环中更改userInput

您需要将userInput = Console.ReadLine()的副本放在While循环的末尾,以便可以输入新值并重新执行逻辑。

此外,您还可以从测试中删除=符号。除非新数字实际更高/更低,否则不会更改最高/最低值。

票数 0
EN

Stack Overflow用户

发布于 2016-02-25 08:36:09

将这一行移动到while循环中

代码语言:javascript
复制
userInput = Console.ReadLine()
票数 0
EN

Stack Overflow用户

发布于 2016-02-25 08:55:16

这是我的解决方案。它检查用户的输入,如果不是整数,他们可以重试或输入-99并退出...您还可以通过更改显示2的几个位置来更改用户输入的数字数量,或者声明一个变量并设置它,然后使用它……

代码语言:javascript
复制
 Public Sub Main()

    Dim userInput As Integer = 0
    Dim lstInteger As New List(Of Integer)

    Console.WriteLine("Input your numbers and when you are done enter -99." & Environment.NewLine & "The app will then give the highest and lowest numbers entered.")

    Do Until userInput = -99
        If Integer.TryParse(Console.ReadLine(),userInput) AndAlso userInput <> -99 Then
            lstInteger.Add(userInput)
            If lstInteger.Count = 2 Then Exit Do
            userInput = 0
            If Integer.TryParse(Console.ReadLine(), userInput) AndAlso userInput <> -99 Then
                lstInteger.Add(userInput)
                If lstInteger.Count = 2 Then Exit Do
            Else
                If userInput = -99 Then Exit Do Else Console.WriteLine("Please enter a number.")
            End If
        Else
            If userInput <> - 99 AndAlso userInput = 0 Then 
                Console.WriteLine("Please enter a number.")
            Else
                Exit Do
            End If 
        End If
    Loop

    If lstInteger.Count = 2 Then 
       Console.WriteLine("The highest number you entered is: " & lstInteger.Max.ToString)
       Console.WriteLine("The lowest number you entered is: " & lstInteger.Min.ToString)
    End If 

    Console.ReadLine()          

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35615415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档