2015-01-21 46 views
0
import java.util.Scanner; 

public class ProgrammaD{ 
    public static void main(String[] args){ 
     Scanner keyboard= new Scanner(System.in); 
     String sequence = args[0]; 
     char chars[] = new char[sequence.length()]; 

     for(int i = 0; i < sequence.length(); i++){ 
      chars[i] = sequence.charAt(i); 
     } 

     System.out.println("Insert a character"); 
     char car = keyboard.next().charAt(0); 
     System.out.println("Insert an integer"); 
     int n = keyboard.nextInt(); 
     System.out.println("Original Array"); 

     for(int z = 0; z < sequence.length(); z++){ 
      System.out.println(chars[z]); 
     } 

     char chars2[] = MetodiD.occorrenzeContenuto(chars, n, car); 
     System.out.println("Edited Array"); 

     for(int k = 0; k < sequence.length(); k++){ 
      System.out.println(chars2[k]); 
     } 

     boolean condition = false; 
     char chars3[] = MetodiD.uniformaContenuto(chars, chars2); 

     for(int x = 0; x < sequence.length(); x++){ 
      if(chars[x] == chars2[x]){ 
       condition = false; 
      } 
      else{ 
       condition = true; 
      } 
     } 

     if(condition){ 
      for(int y = 0; y < sequence.length(); y++){ 
       System.out.println(chars3[y]); 
      } 
     } 
    } 
} 

我不能執行最後一條if語句,我不明白爲什麼。有人能幫助我如何弄清楚嗎?我想讓程序打印在MetodiD.uniformaContenuto()中返回的數組,但是我無法從這段代碼中得到任何東西。不能在這個if-else語句中輸入

這裏的其他類:

public class MetodiD{ 
    public static char[] occorrenzeContenuto(char par[], int a, char b){ 
     int contatore = 0; 
      for(int i = 0; i < par.length; i++){ 
       if(par[i] != b){ 
        contatore++; 
       }else{ 
       } 
      } 
      if(contatore > a){ 
       for(int k = 0; k < par.length; k++){ 
        if(par[k] != b){ 
         par[k] = determinaCarattere(a, b); 
        }else{ 
       } 
      } 
      }else{ 
      } 
     return par; 
    } 

    public static char determinaCarattere(int n, char b){ 
     int modificato; 
      if(n%2==0){ 
       modificato = (int)b - n; 
      }else{ 
       modificato = (int) b + n; 
      } 
     char modificato2 = (char)modificato; 
     return modificato2; 
    } 

    public static char[] uniformaContenuto(char par1[], char par2[]){ 
     char rit[] = new char[par1.length]; 
      for(int y = 0; y < par1.length; y++){ 
       if(par1[y]<par2[y]){ 
        rit[y] = par1[y]; 
       }else{ 
        rit[y] = par2[y]; 
       } 
      } 
     return rit; 
    } 
} 
+0

打印語句總是有幫助的(如果你沒有調試器) – 2015-01-21 00:41:59

+0

你的方法做什麼?我傾向於認爲你的「條件」布爾總是返回false – 2015-01-21 00:47:51

+1

你在'for'循環中分配'condition',只有最後一個影響'condition'的值,請檢查它! – BornToCode 2015-01-21 00:48:21

回答

0

字符被設置occorrenzeContenuto內,然後返回並分配給chars2,所以他們永遠是平等的。嘗試將此版本交換爲創建新陣列的版本,並在開始時將參數複製到其中,然後將其用於返回值。

public static char[] occorrenzeContenuto(char par[], int a, char b){ 
char[] retVal = new char[par.length]; 
System.arraycopy(par,0,retVal,0,par.length); 
int contatore = 0; 
for(int i = 0; i < par.length; i++){ 
    if(par[i] != b){ 
    contatore++; 
    }else{ 
    } 
} 
if(contatore > a){ 
    for(int k = 0; k < par.length; k++){ 
    if(par[k] != b){ 
     retVal[k] = determinaCarattere(a, b); 
    }else{ 
    } 
    } 
}else{ 
} 
return retVal; 
} 

的原因的問題是,數組參數保持原來的實例的引用,類似的對象做,而不是個人如何原始的像一個char的行爲,如解釋在這裏:Java pass by reference