这是我编程课程的第三周,我被卡住了。我可以让它运行我只是不能输入超过1个数字。这是一个vb控制台应用程序。
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
答案 0 :(得分:0)
在循环开始之前,您只执行ReadLine
一次,因此您只能输入一个数字。目前看来,如果你没有输入-99,那么你就会有一个无限循环,因为你无法在循环中改变userInput
。
您需要在userInput = Console.ReadLine()
循环的末尾放置While
的副本,以便输入新值并重新执行逻辑。
此外,您还可以从测试中删除=
符号。除非新数字实际更高/更低,否则无法改变最高/最低。
答案 1 :(得分:0)
在while循环中移动此行
APPLY
答案 2 :(得分:0)
这是我的解决方案。它会检查用户的输入,如果它不是整数,则可以再次尝试或输入-99然后退出...您还可以通过更改显示2
的几个位置来更改用户输入的数量,或声明一个变量并设置它,然后使用它......
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