我应该使用什么条件? [英] What condition should I use?

查看:150
本文介绍了我应该使用什么条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过读取十(10)个整数值并将这些值存储在数组中来修订示例问题#3.确定并显示输入的素数的数量.

我的代码有什么错误?

请帮助T-T

我尝试过的事情:

公共课程表格1
私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.Click
Dim MyArray(9),将Ctr设置为整数
Dim j,k作为整数
对于ctr = 0到9
MyArray(ctr)= InputBox(输入数字:")
ListBox1.Items.Add(MyArray(ctr))
下一个

对于j = 2 To(MyArray(ctr)-1)
如果MyArray(ctr)Mod j = 0那么
k = k + 1
如果结束
下一个
TextBox1.Text = k
结束子
End Class

Revise the Sample Problem #3 by reading ten (10) integer values and storing these values in an array. Determine and display the number of prime numbers entered.

What is the error in my code?

Please help T-T

What I have tried:

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MyArray(9), ctr As Integer
Dim j, k As Integer
For ctr = 0 To 9
MyArray(ctr) = InputBox("Input number :")
ListBox1.Items.Add(MyArray(ctr))
Next

For j = 2 To (MyArray(ctr) - 1)
If MyArray(ctr) Mod j = 0 Then
k = k + 1
End If
Next
TextBox1.Text = k
End Sub
End Class

推荐答案

报价:

我的代码有什么错误?


当您寻求帮助时,最好给出错误消息.

第一个错误:您检查不存在的MyArray(ctr).索引超出范围.

第二个错误:您是在计算除数,而不是在计算素数.

多美学习调试器:
有一个工具可以让您查看代码的作用,其名称为 debugger .这也是一个很好的学习工具,因为它可以向您显示现实,并且您可以看到哪个期望与现实相匹配.
当您不了解代码在做什么或为什么要做什么时,答案是调试器.
使用调试器查看您的代码在做什么.只需设置一个断点并查看您的代码执行情况,调试器就可以让您一行一行地执行代码,并在执行时检查变量.

调试器-维基百科,免费百科全书 [ Visual Basic/Visual Studio视频教程-基本调试-YouTube [面向初学者的Visual Basic .NET编程-断点和调试工具 [


When you ask for help, it is a good idea to give the error message.

First error: you check MyArray(ctr) which does not exist. index out of bound.

second error: you are counting divisors instead of counting prime numbers.

Tome to learn debugger:
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don''t understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don''t find bugs, it just help you to. When the code don''t do what is expected, you are close to a bug.


存在多个问题.

您可能不希望在检查质数之前完成输入循环.将第一个Next移到另一个后面(适当地缩进代码行将使其更加明显).

您的素数检测和计数将无法正常工作.认识到数字不是质数时,应该打破循环,仅在没有发生质数时才递增k.实际上,当值不是素数时,您实际上正在递增k,并且对于单个值可能会发生多次.

更好的方法是编写一个函数来检查传递的数字是否为素数.


如ppolymorphe所述:
如果您要求用户输入10个数字,则必须提供足够的空间来存储数字.
[/EDIT]
There are multiple problems.

You probably don''t want the input loop to be finished before checking for prime numbers. Move the first Next just behind the other (properly indenting code lines would make this more obvious).

Your prime number detection and counting will not work as expected. You should break the loop when recognising that a number is not a prime and increment k only when that does not happen. You are actually incrementing k when the value is not a prime and that may happen multiple times for a single value.

A better method would be writing a function to check if the passed number is a prime or not.


As already mentioned by ppolymorphe:
If you ask the user for 10 numbers you must provide enough space to store the numbers.
[/EDIT]


这篇关于我应该使用什么条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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