VB编程.整数数组和素数 [英] VB programming. Integer array and prime numbers

查看:51
本文介绍了VB编程.整数数组和素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和堆栈溢出博客的新手,所以希望我能正确地遵循该做什么和不该做什么".

I'm new to programming and stack overflow blogs, so hopefully I am following the 'do's and dont's' properly.

我收到了一道作业问题,要求我将 5 个整数存储在一个数组中,并确定它们是否是素数.

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.

我的问题如下:

  1. 如何将它们存储到整数数组中?

  1. 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!

推荐答案

这个问题的基本答案是接受用户输入,并在 2 和用户输入之间的每个数字上运行一个 mod.

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!

这篇关于VB编程.整数数组和素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆