2014-06-06 66 views
-1

我正在嘗試做一個遞歸函數,該函數根據另一個int數組上的規則檢查1個int數組。在遞歸過程中從函數中獲取值

例如:

pattern (the array with the rules)={1,0,2} 
a={9,10,15} 

規則:

1 - one digit number. 
2 - 2 digits number. 
0 - one digit number or 2 digits number. 

a[0]=9, pattern[0]=1. 
9 is 1 digit number -> true. 

a[1]=10, pattern[1]=0 
10 is 2 digits number -> true. 

函數返回真,如果它找到一個陣列中的圖案的序列。 如果兩個數字都沒有問題,第三個數字沒有問題,它會繼續在「a」數組上搜索,直到找到符合該模式的行中的3個數字爲止。

我沒有找到一種方法來停止遞歸,當它成功地找到模式的序列,並繼續前進,直到我得到一個不受歡迎的錯誤。

沒能做到這一點還是想上一個辦法讓它完成,然後得到真實的(總是在最後假)

非常感謝您的幫助!

public static boolean match(Integer[] a,Integer[] pattern) 
    { 
     int _counter = 0; 
     int _variNum = 0; 
     Integer[] _copyA= new Integer[a.length];_copyA=a; 
     Integer[] _copyPattern= new Integer[pattern.length];_copyPattern=pattern; 
     return match(_copyA,_copyPattern,pattern.length,a,pattern); 
    } 


    public static boolean match(Integer[] secA,Integer[] secPattern,int originPatternLength,Integer[] originA,Integer[] originPattern) 
    { 
     boolean success=false; 
     System.out.println("\n"); 
     System.out.println("First array: "+Arrays.toString(secPattern)+" Second array: "+Arrays.toString(secA)+" to the rules..."); 
     System.out.println("_variNum: *"+_variNum+"* secPattern.length: *"+secPattern.length+"*  counter: "+_counter); 

     success=(((originA.length-_counter)+_variNum)<originPatternLength)?false:true; 
     success=(originPatternLength ==_variNum)?true:false; 

     _counter++; 
     if(secPattern.length>0) 
     { 
      switch ((secPattern[0])) 
       { 
        case 0: 
         if(!(secA[0]>-100 && secA[0]<100)) 
         { 
          Integer[] newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0; 
          match(newArr,secPattern,originPatternLength,originA,originPattern); 
         } 
         _variNum++; 
         Integer[] newArr = Arrays.copyOfRange(secA, 1, secA.length); 
         Integer[] newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length); 
         match(newArr,newPArr,originPatternLength,originA,originPattern); 

        break; 
        case 1: 
         if(!(secA[0]>-10 && secA[0]<10)) 
         { 
          newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0; 
          match(newArr,secPattern,originPatternLength,originA,originPattern); 
         } 
         _variNum++; 
         newArr = Arrays.copyOfRange(secA, 1, secA.length); 
         newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length); 
         return match(newArr,newPArr,originPatternLength,originA,originPattern); 
        break; 
        case 2: 
         if(!(secA[0]>-100 && secA[0]<100&&(secA[0]>10||secA[0]<-10))) 
         { 
          newArr = Arrays.copyOfRange(secA, 1, secA.length);_variNum=0;_counter=0; 
          match(newArr,secPattern,originPatternLength,originA,originPattern); 
         } 
         _variNum++; 
         newArr = Arrays.copyOfRange(secA, 1, secA.length); 
         newPArr = Arrays.copyOfRange(secPattern, 1, secPattern.length); 
         match(newArr,newPArr,originPatternLength,originA,originPattern); 
        break; 
        default: 
      } 
     } 

     return success; 
    }` 
+0

對我來說,這聽起來像一個完美的重複任務。爲什麼你想首先使用遞歸? – aRestless

+0

爲什麼你使用遞歸?兩個嵌入式'for'循環將更簡單,更快速,更安全地完成任務。 – CiaPan

+0

這是Uni'任務:) – user3652172

回答

0

嘗試f.f.g代碼:

 public AnyClass(){ 
     private int a[]; 
     private int b[]; 
     String y ; 


     public AnyClass(int c){ 
     a = new int[]{1,0,2}; 
     b = new int[c]; 
     } 

     public void input(){ 
     for(int i = 0; i < c; i++){ 
     b[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number")); 
     } 



     public String patternLocator(){ 
     for(int i = 0; i < b.length; i++){ 
     int c2 = 0; 
     y = ""; 
     for(int c = i; c < i+3; c++){ 

     if(a[c2] == 1){ 
     if(b[c] < 10){ 
     y = y + "yes"; 
     } 

    if(a[c2] == 2){ 
    if(b[c] > 9 && b[c] < 100){ 
     y = y + "yes"; 
    } 
    } 


    if(a[c2] == 0){ 
    if(b[c] < 100){ 
    y = y + "yes"; 
    } 
    } 
    } 
    if(y.equals("yesyesyes")){ 
    y = true; 
    return y; 
    break; 
    } 
    c2++; 
    } 
    y = "false"; 
    return y; 
    } 

public static void main(String args[]){ 
int length = Integer.parseInt("Enter length of pattern you are about to enter"); 
AnyClass ob = new AnyClass(lenght); 
ob.input; 
String ans = ob.patternLocator(); 
if(ans = "true"){ 
System.out.println("pattern found"); 
} 
else{ 
System.out.println("Pattern not found"); 
} 
} 

     }