0

我需要一些关于如何为我正在编写的迷你搜索引擎编写主要方法的指针。这是我的代码:

public class StringSearch {   
    private String s1 = "ACTGACGCAG";
    private String s2 = "TCACAACGGG";
    private String s3 = "GAGTCCAGTT";

    public static void main(String[] args) {         
        System.out.println("Welcome!  The strings you started with are:\n" + s1 + "\n" + s2 + "\n" + s3);
    }

    public void search() {
        do {
            for(int i = 0; i < s1.length() - 4; i++) {
                int d = 0;
                String subStr = s1.substring(0 + i, 4 + i);
                do{ 
                    for (int iSub = 0; iSub < 4; i++){
                        if (subStr.charAt(iSub) != (subStr.charAt(iSub))) {
                            d += 1;
                        }
                    }
                }while(d < 2);
                if(s2.contains(subStr) && s3.contains(subStr)) {
                    System.out.println(subStr + "is in all 3 lists.");
                }
            }
        }while (s1.length() - 4 < 6);
        System.out.println("Task Complete.");        
    }
}

这个想法是我有一组 3 个字符串开始,我需要创建一个 4 个字符的子字符串并将其与所有 3 个字符串进行比较,以查看它是否包含在每个字符串中,并且至少有 3/4 个字母匹配。例如,如果我取 s1 (ACTG) 的前 4 个字符,那么 'CCTG'、'ACAG'、'ACTA'、'AATG' 都是有效的搜索结果并会被返回。

我遇到的问题是主要方法。我究竟应该如何在语法上实例化搜索方法?我试过 StringSearch s1 = new StringSearch(); 然后是 s1.search(); 但没有得到任何结果。此外,当我尝试在 println 中引用原始字符串时,它说我无法从静态上下文中引用它们。Java新手在这里,将不胜感激具体的帮助。

4

1 回答 1

0

您不能直接在静态方法中引用字段。首先,您需要像这样创建它的对象:-

public static void main(String[] args) {  
    StringSearch stringSearch = new StringSearch();
    stringSearch.search();
    System.out.println("Welcome!  The strings you started with are:\n" + stringSearch.s1 + "\n" + stringSearch.s2 + "\n" + stringSearch.s3);
}

对于搜索方法的问题,您需要调试您的方法。

脚步:-

  1. 如果您使用的是 eclipse,请双击左侧的行号或右键单击并选择 Toggle breakpoint 作为您希望主线程停止的位置。

  2. 右键单击程序并使用调试作为选项。

  3. 使用 F5、F6、F7 或 F8 键进行调试

F5 执行当前选定的行并转到程序中的下一行。如果选定的行是一个方法调用,调试器会进入相关的代码。

F6 跳过调用,即它执行一个方法而不在调试器中单步执行该方法。

F7 跳到当前执行方法的调用者处。这完成了当前方法的执行并返回给该方法的调用者。

F8 告诉 Eclipse 调试器继续执行程序代码,直到到达下一个断点或观察点。

于 2015-08-20T03:28:26.993 回答