控制台应用素数

时间:2015-01-19 01:46:11

标签: vb.net console-application

我应该创建一个表单程序和一个控制台程序来确定素数。我已经完成了表单程序,但我遇到了Console程序的问题。

我觉得我几乎让它工作,唯一的事情是有时它只能每秒输入一次,有时错误仍会导致程序崩溃,即使我有一个On Error语句。

如果有人能向我解释为什么会这样,我将不胜感激。我想明白为什么它正在做它正在做的事情。我应该使用Case语句而不是Do Loop吗?

当前代码:

Module Module1

    Sub Main()

        System.Console.WriteLine("Enter a number from 1 to 10000 to find out if it's prime, Type END to exit")
ExitHere:
        Do
            On Error GoTo ErrorHandler
            Dim UserInput As Integer = Integer.Parse(Console.ReadLine())
            Dim IsItPrime As Boolean = False
            Dim i As Integer


            For i = 2 To (UserInput - 1)
                If UserInput Mod i = 0 Then
                    IsItPrime = False
                    Exit For
                End If
            Next i
            If i = UserInput Then
                System.Console.WriteLine(UserInput & " is a Prime Number.")
            Else
                System.Console.WriteLine(UserInput & " is not a prime number.")
            End If


        Loop Until Console.ReadLine = "End"
        Exit Sub
ErrorHandler:
        Console.WriteLine("There seems to have been an error.")
        GoTo ExitHere


    End Sub

End Module

3 个答案:

答案 0 :(得分:0)

我写的更像是:

Sub Main()
    Dim response As String
    Do
        Console.Write("Enter an integer from 1 to 10000, or `END` to exit: ")
        response = Console.ReadLine().ToUpper
        If response <> "END" Then
            Dim UserInput As Integer
            If Integer.TryParse(response, UserInput) Then
                If UserInput >= 1 AndAlso UserInput <= 10000 Then
                    Dim IsItPrime As Boolean = True ' assume it's prime until proven otherwise
                    If UserInput > 3 Then
                        For i As Integer = 2 To (UserInput - 1)
                            If UserInput Mod i = 0 Then
                                IsItPrime = False
                                Exit For
                            End If
                        Next i

                    End If
                    If IsItPrime Then
                        System.Console.WriteLine(UserInput & " is a Prime Number.")
                    Else
                        System.Console.WriteLine(UserInput & " is NOT a prime number.")
                    End If
                Else
                    Console.WriteLine("Out of Valid Range")
                End If
            Else
                Console.WriteLine("Invalid Integer.")
            End If
        End If
    Loop While response <> "END"
End Sub

答案 1 :(得分:0)

你有两个ReadLine,你应该只有一个。删除Loop Until中的ReadLine。存储第一个ReadLine的值,并确定它是数字还是结束。

Dim input As String = Console.ReadLine()
Dim inputNumber As Integer

If input = "End" Then
    Exit Do
Else If Integer.TryParse(input, inputNumber) Then
    ' It's a number
Else
    ' Invalid input
End If

答案 2 :(得分:0)

    Sub Main()

    Dim n As Integer = 1

    Dim counter As Integer

    Dim x As Integer

    Console.Write("Enter A Number : ")

    x = Console.ReadLine

    While n <= x

        If x Mod n = 0 Then
            counter = counter + 1

        End If
        n = n + 1
    End While
    If counter <= 2 Then
        Console.WriteLine("This Number is a Prime")
    Else
        Console.WriteLine("This Number is a Composite")
    End If

    Console.ReadLine()

End Sub