问题
I'm new to programming and stack overflow blogs, so hopefully I am following the 'do's and dont's' properly.
I have been given an assignment question asking me to store 5 integers in an array and to determine if they are a prime number or not.
The questions I have are as follows:
How do I store them into an integer array?
How do I make my program divide every input by every number less than the input?
The code I have written so far is this:
Sub Main()
Dim a, b, c, d, e As Integer
Dim isPrime As Boolean = True
Console.WriteLine("Please enter a value for a: ")
a = Console.ReadLine
Console.WriteLine("Please enter a value for b: ")
b = Console.ReadLine
Console.WriteLine("Please enter a value for c: ")
c = Console.ReadLine
Console.WriteLine("Please enter a value for d: ")
d = Console.ReadLine
Console.WriteLine("Please enter a value for e: ")
e = Console.ReadLine
If a Mod (a - 1) > 0 Or a = 2 And a <> 0 Then
Console.WriteLine("a is a prime number")
ElseIf a Mod (a - 1) = 0 Then
Console.WriteLine("a is not a prime number")
End If
If b Mod (b - 1) > 0 Or b = 2 And b <> 0 Then
Console.WriteLine("b is a prime number")
ElseIf b Mod (b - 1) = 0 Then
Console.WriteLine("b is not a prime number")
End If
If c Mod (c - 1) > 0 Or c = 2 And c <> 0 Then
Console.WriteLine("c is a prime number")
ElseIf c Mod (c - 1) = 0 Then
Console.WriteLine("c is not a prime number")
End If
If d Mod (d - 1) > 0 Or d = 2 And d <> 0 Then
Console.WriteLine("d is a prime number")
ElseIf d Mod (d - 1) = 0 Then
Console.WriteLine("d is not a prime number")
End If
If e Mod (e - 1) > 0 Or e = 2 And e <> 0 Then
Console.WriteLine("e is a prime number")
ElseIf e Mod (e - 1) = 0 Then
Console.WriteLine("e is not a prime number")
End If
Console.ReadKey()
End Sub
Learning all of this stuff with the prior knowledge provided has made things difficult, so any help would be greatly appreciated!
回答1:
Dim a, b, c, d, e As Integer
Console.WriteLine("Please enter a value for a: ")
a = Console.ReadLine
Console.WriteLine("Please enter a value for b: ")
b = Console.ReadLine
Console.WriteLine("Please enter a value for c: ")
c = Console.ReadLine
Console.WriteLine("Please enter a value for d: ")
d = Console.ReadLine
Console.WriteLine("Please enter a value for e: ")
e = Console.ReadLine
Dim intary() As Integer = {CInt(a), CInt(b), CInt(c), CInt(d), CInt(e)}
For Each number As Integer In intary
Dim prime As Boolean = True
For x As Integer = 2 To number - 1
If number Mod x = 0 Then
prime = False
Exit For
End If
Next
If prime Then
Console.WriteLine(number.ToString & " IS a prime number")
Else
Console.WriteLine(number.ToString & " IS NOT a prime number")
End If
Next
The integer array is created in the first line. Then, the concept is you would want to iterate through each item in the array, and run a prime number test on it. So, we have a for loop that will run our prime number test on each number in the array. For each of the numbers, we divide the test-number by every number from two all the way up to the given test-number and check the mod remainder. If any of those return zero besides the number itself, then we do not have a prime number. In the example shown, I am just printing a simple line stating whether or not each number passed or failed the test...hope this helps and good luck learning your new language!
Side note, you could dim the array and add each new number as it's retrieved from the user, but you would need to redim or resize the array each time. The example I wrote just uses the input logic you already had in place. If the assignment didn't explicitly ask for an array, you'd be able to write a much cleaner solution if you were to use a list.
回答2:
The basic answer to this question is to take the users inputs, and to run a mod on each of these numbers between 2 and the users input.
To the people who are starting out like me, my advice is that you don't differ from the basic idea and it will all eventually make sense!
来源:https://stackoverflow.com/questions/39113097/vb-programming-integer-array-and-prime-numbers