我迷路了,請問有人能幫我嗎?java遞歸行
我想我的函數「oddEvenRow」檢查行索引0,2,4中的值是否爲奇數。如果是的話返回true,如果不是返回false
這是我寫的代碼:
public class Matrix
{
public static int temp=0;
public static boolean oddEvenRow (int[][]a, int r, int c, int count)
{
if(r>4&&count==12) {
temp=1;
return false;
}
if(a[r][c]%2==0) {
temp=1;
return false;
}
else {
count++;
if(c==3)
oddEvenRow(a,r+2,0,count);
else
oddEvenRow(a,r,c+1,count);
return true;
}
}
public static void main(String args[])
{
int r=0;
int c=0;
int count=0;
int[][]a=new int[5][4];
a[0][0]=1;
a[0][1]=3;
a[0][2]=7;
a[0][3]=15;
a[1][0]=4;
a[1][1]=15;
a[1][2]=2;
a[1][3]=9;
a[2][0]=11;
a[2][1]=21;
a[2][2]=1;
a[2][3]=45;
a[3][0]=8;
a[3][1]=15;
a[3][2]=8;
a[3][3]=12;
a[4][0]=7;
a[4][1]=3;
a[4][2]=25;
a[4][3]=21;
System.out.println(oddEvenRow(a,r,c,count));
}
}
要發佈的事實,而不是一個問題。 – Maroun
我用'temp = 0'迷路了...又是什麼問題? – radimpe
爲了理解遞歸,你必須首先理解遞歸。 – Fildor