学习 Java 调试

发布于 2024-07-25 23:55:58 字数 3992 浏览 3 评论 0原文

我正在学习在 Netbeans 上使用 JPDA 并解决 Sphere 的 Prime Generator 问题在线法官。

我一直在阅读 netbeans.org 上的本教程他JPDA,但没有发现它有多大帮助。

此代码基于 starblue 此处,像这样运行:

2
1 10
//here the primes between 1 and 10 should print 
3 5
//here the primes between 3 and 5 should print 




package sphere;

/**
 *
 * @author Administrator
 */
//import java.util.ArrayList;
import java.util.BitSet;
import java.lang.Math.*;
import java.util.ArrayList;

public class Main
{

  public static int ApproximateNthPrime(int nn)
{
    double n = (double)nn;
    double p;
    if (nn >= 7022)
    {
        p = n * Math.log(n) + n * (Math.log(Math.log(n)) - 0.9385);
    }
    else if (nn >= 6)
    {
        p = n * Math.log(n) + n * Math.log(Math.log(n));
    }
    else if (nn > 0)
    {
        p = new int[] { 2, 3, 5, 7, 11 }[nn - 1];
    }
    else
    {
        p = 0;
    }
    return (int)p;
}

// Find all primes up to and including the limit
public static BitSet SieveOfEratosthenes(int limit)
{
    final BitSet primes = new BitSet();
    primes.set(0,false); 
    primes.set(1,false); 
    primes.set(2,limit,true); 

    for (int i =0; i*i<limit;i++)
    {
        if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);// hace que el indice j sea false (no primo)
            }

        }

    }
    return primes;
}

public static ArrayList<Integer> GeneratePrimesSieveOfEratosthenes(int n)
{
    int limit = ApproximateNthPrime(n);
    BitSet bits = SieveOfEratosthenes(limit);
    ArrayList <Integer> primes = new ArrayList<Integer>();
    for (int i = 0, found = 0; i < limit && found < n; i++)
    {
        if (bits.get(i))
        {
            primes.add(i);
            found++;
        }
    }
    return primes;
}





  public static void main (String[] args) throws java.lang.Exception
  {
     java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
     String s;

     s= r.readLine();

     int test_cases = Integer.parseInt(s);


     int case_counter =0;

     while (case_counter<test_cases) {

        // System.out.println(s);
         s = r.readLine();

         String [] splitted = s.split(" ");

         int lower_bound = Integer.parseInt(splitted[0]);
         int upper_bound = Integer.parseInt(splitted[1]);



        ArrayList <Integer> primesList=  GeneratePrimesSieveOfEratosthenes(upper_bound);



         for (int i =0; i<primesList.size();i++){
            if (primesList.get(i)<=lower_bound)System.out.println(primesList.get(i));
         }


         case_counter++;

         System.out.println(" "); // space that separates test cases

     }
  }
}

我知道 ArrayList primesList 没有被初始化,我对这段代码表示怀疑,因为老实说,我不太理解它:

if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);
            }

        }

我想到使用条件断点这里的条件是:

primes.get(j)==false

但我不确定我是否能够通过这种方式获得有意义的信息。 这些是我得到的屏幕:

alt text http://img525.imageshack.us /img525/6238/breakpoints.jpg

替代文本 http://img98. imageshack.us/img98/5262/watchesz.jpg

我不知道如何从中获取有用的信息。

我的问题是:

a)我想观看素数 BitSet 经历这个循环的过程。

我该怎么做?

b) < strong>这段代码究竟有什么问题? 您是如何使用调试器发现它的?

请提及分步过程。

I'm both learning to use the JPDA on Netbeans and solving the Prime Generator problem of Sphere's Online Judge.

I've been reading this tutorial on netbeans.org about he JPDA, but haven't found it of much help.

This code, which is based on a Sieve of Eratostenes implementation provided by starblue here, is running like this:

2
1 10
//here the primes between 1 and 10 should print 
3 5
//here the primes between 3 and 5 should print 




package sphere;

/**
 *
 * @author Administrator
 */
//import java.util.ArrayList;
import java.util.BitSet;
import java.lang.Math.*;
import java.util.ArrayList;

public class Main
{

  public static int ApproximateNthPrime(int nn)
{
    double n = (double)nn;
    double p;
    if (nn >= 7022)
    {
        p = n * Math.log(n) + n * (Math.log(Math.log(n)) - 0.9385);
    }
    else if (nn >= 6)
    {
        p = n * Math.log(n) + n * Math.log(Math.log(n));
    }
    else if (nn > 0)
    {
        p = new int[] { 2, 3, 5, 7, 11 }[nn - 1];
    }
    else
    {
        p = 0;
    }
    return (int)p;
}

// Find all primes up to and including the limit
public static BitSet SieveOfEratosthenes(int limit)
{
    final BitSet primes = new BitSet();
    primes.set(0,false); 
    primes.set(1,false); 
    primes.set(2,limit,true); 

    for (int i =0; i*i<limit;i++)
    {
        if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);// hace que el indice j sea false (no primo)
            }

        }

    }
    return primes;
}

public static ArrayList<Integer> GeneratePrimesSieveOfEratosthenes(int n)
{
    int limit = ApproximateNthPrime(n);
    BitSet bits = SieveOfEratosthenes(limit);
    ArrayList <Integer> primes = new ArrayList<Integer>();
    for (int i = 0, found = 0; i < limit && found < n; i++)
    {
        if (bits.get(i))
        {
            primes.add(i);
            found++;
        }
    }
    return primes;
}





  public static void main (String[] args) throws java.lang.Exception
  {
     java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
     String s;

     s= r.readLine();

     int test_cases = Integer.parseInt(s);


     int case_counter =0;

     while (case_counter<test_cases) {

        // System.out.println(s);
         s = r.readLine();

         String [] splitted = s.split(" ");

         int lower_bound = Integer.parseInt(splitted[0]);
         int upper_bound = Integer.parseInt(splitted[1]);



        ArrayList <Integer> primesList=  GeneratePrimesSieveOfEratosthenes(upper_bound);



         for (int i =0; i<primesList.size();i++){
            if (primesList.get(i)<=lower_bound)System.out.println(primesList.get(i));
         }


         case_counter++;

         System.out.println(" "); // space that separates test cases

     }
  }
}

I know that the ArrayList primesList isn't getting initialized and I'm suspicious of this bit of code, cause honestly, I don't quite understand it:

if (primes.get(i))
        {
            for (int j=i*1; j<limit;j+=1)
            {
                primes.clear(j);
            }

        }

It occurred to me to use a conditional breakpoint here with the condition of:

primes.get(j)==false

But I'm not sure if I'm able to get meaningful info this way. These are the screens I'm getting:

alt text http://img525.imageshack.us/img525/6238/breakpoints.jpg

alt text http://img98.imageshack.us/img98/5262/watchesz.jpg

I don't know how to get useful info out of this.

My questions are:

a)I want to watch the primes BitSet as its going through this loop.

How do I do that?

b) What's exactly wrong with this code?
How did you spot it using the debugger?

Please, mention the step-by-step process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-08-01 23:55:59

因此,我提取了以下方法:

    private static void printPrimes(int lower_bound, int upper_bound) {
    ArrayList<Integer> primesList = GeneratePrimesSieveOfEratosthenes(upper_bound);

    for (int i = 0; i < primesList.size(); i++) {
        if (primesList.get(i) <= lower_bound)
            System.out.println(primesList.get(i));
    }
}

并将 main() 方法更改为仅使用几个任意参数(10 和 100)来调用该方法,因为我不想乱搞同时控制台和调试器。 然后(我使用的是 Eclipse)在 ApproximateNthPrime()、SieveOfEratosthenes() 和 GeneratePrimesSieveOfEratosthenes() 的开头和结尾处放置普通断点> 确保他们被呼叫。 (顺便说一句,与 C# 不同,Java 约定方法名称以小写字母开头。)

所有这一切都无需费心去理解代码。 :) 然而,在第一次运行之后,很明显问题是由 SieveOfEratosthenes() 生成的 BitSet 始终为空(或者更确切地说,始终完全为空) )。 我没有使用过 NetBeans 调试器,但我怀疑“本地变量”选项卡是您的朋友。

我不会为你做作业。 :) 但是埃拉托斯特尼筛法的思想是跳过素数,只消除非素数。 检查您的 SieveOfEratosthenes() 方法并问问自己:它什么时候会跳过一个数字?

So, I extracted out the following method:

    private static void printPrimes(int lower_bound, int upper_bound) {
    ArrayList<Integer> primesList = GeneratePrimesSieveOfEratosthenes(upper_bound);

    for (int i = 0; i < primesList.size(); i++) {
        if (primesList.get(i) <= lower_bound)
            System.out.println(primesList.get(i));
    }
}

and changed the main() method to just call that with a couple of arbitrary arguments (10 and 100), because I didn't want to mess around with the console and the debugger at the same time. I then (I'm using Eclipse) put ordinary breakpoints at the beginning and end lines of ApproximateNthPrime(), SieveOfEratosthenes() and GeneratePrimesSieveOfEratosthenes() to make sure they were being called. (By the way, Java convention, unlike C#, is for method names to start with a lower-case letter.)

All that was without bothering to understand the code. :) However, after the first run-through, it was pretty clear that the problem is that the BitSet produced by SieveOfEratosthenes() is always empty (or rather, always entirely false). I haven't used the NetBeans debugger, but I suspect the "Local Variables" tab is your friend here.

I'm not going to do your homework for you. :) But the idea of the Sieve of Eratosthenes is to skip the prime numbers and only eliminate the non-primes. Examine your SieveOfEratosthenes() method and ask yourself: when will it skip a number?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文