我需要一些指導,以瞭解如何編寫我正在編寫的迷你搜索引擎的主要方法。這裏是我的代碼:編寫此搜索功能的主要方法
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新手在這裏,具體的幫助將不勝感激。
推薦閱讀:[**如何調試小程序**](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – durron597
特別是,'new StringSearch ().search()'在你的主要方法中是正確的;它不起作用的原因是你的實際搜索方法可能有問題。閱讀鏈接。 – durron597
請勿添加鏈接,因爲這些鏈接可能會在任何一天脫機。其他人將來無法從中受益。 –