vb.net 控制台应用程序质数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28016856/
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 18:41:10  来源:igfitidea点击:

Console application prime number

vb.netconsole-application

提问by Stan Harris

I'm supposed to create a form program and a console program to determine prime numbers. I've completed the form program but i'm having trouble with the Console program.

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

I feel i almost got it working, the only thing is sometimes it only works every second input, and sometimes errors still crash the program even though i have an On Error statement.

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

If anyone can explain to me why this is happening I would be grateful. I want to understand why it's doing what it's doing. Should I maybe use a Case Statement rather than a Do Loop?

如果有人能向我解释为什么会发生这种情况,我将不胜感激。我想了解它为什么要做它正在做的事情。我应该使用 Case 语句而不是 Do 循环吗?

Current Code:

当前代码:

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

回答by Idle_Mind

I'd write it something more like:

我会写一些更像:

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

回答by the_lotus

You have two ReadLine, you should only have one. Remove the ReadLine inside the Loop Until. Store the value of the first ReadLine and determine if it's a number or End.

你有两个 ReadLine,你应该只有一个。删除循环直到内的 ReadLine。存储第一个 ReadLine 的值并确定它是数字还是 End。

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

回答by KUDUS

    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