使用do while循环来确定素数? [英] Using a do while loop to determine a prime number?

查看:576
本文介绍了使用do while循环来确定素数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个简单的程序来使用do while循环来确定素数但是无法获得正确的输出。任何帮助,将不胜感激。我觉得这与我的循环中的更新有关吗?



我尝试了什么:



  public   class  DoWhile { 
public static void main ( String [] args)
{
int num = 13 ;
do
{
System。 out .println(num + 是素数);
num ++;
}
while (num%2 == 1 );

if (num%2 == 1
{
do
{
System。 out .println(num + < span class =code-string>
不是素数);
num ++;
}
while (num%2 == 0 );
}
}
}

解决方案

不,它与那不是一个素数检测器算法,甚至关闭。

看看这个: Eratosthenes筛选 - 维基百科 [ ^ ]并考虑尝试实施该功能。

不,有一些更正,这个程序会判断一个数字是奇数还是偶数。

这个你的程序应该如何看待:

  public   class  DoWhile {
public static void main( String [] args)
{
int num = 13;
int last = 100;
do
{
if (IsPrime(num))
{
System.out.println(num + 是素数 );

}
else
{
System.out.println(num + 不是素数);

}
num ++;
}
while (num< == last);
}
}



您需要定义函数 IsPrime ,它将检查是否数字是否为素数。



如果你很难理解你的程序做什么,请使用调试器,它会告诉你。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/ javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


Hi, I have wrote a simple program to determine a prime number using a do while loop but am having trouble getting the correct output. Any help would be appreciated. I have a feeling it is something to do with the updates in my loops?

What I have tried:

public class DoWhile{
    public static void main(String[]args)
    {
        int num=13;
        do
        {
            System.out.println(num + " is a prime number");
            num++;
        }
        while(num%2==1);
        
        if(num%2==1)
        {
        do
        { 
            System.out.println(num+ " is NOT a prime number");
            num++;
        }
        while (num%2==0);
    }
}
}

解决方案

No, it has to do with that not being a prime number detector algorithm, or even close.
Have a look at this: Sieve of Eratosthenes - Wikipedia[^] and consider trying to implement that.


No, with some corrections this program will tell if a number is odd or even.
This how your program should look:

public class DoWhile{
    public static void main(String[]args)
    {
        int num=13;
        int last=100;
        do
        {
            if(IsPrime(num))
            {
                System.out.println(num + " is a prime number");

            }
            else
            {
                System.out.println(num+ " is NOT a prime number");

            }
            num++;
        }
        while(num <==last);
    }
}


You need to define the function IsPrime that will check if a number is prime or not.

If you have difficulties to understand what you program do, use the debugger, it will show you.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这篇关于使用do while循环来确定素数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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