2017-09-03 59 views
-1

我正在多個字符串輸入隨機交換沒有使用臨時變量。Java中的這個交換程序有什麼問題,爲什麼以及我應該怎麼做?

但是,當我輸入,這種情況發生了幾次:

I can't embed images yet...

這種情況更頻繁......(注意,第一輸出始終爲空,有些輸出偶爾重複)

我的代碼:

import java.util.Arrays; 
import java.util.Scanner; 

public class myFile { 
    public static boolean contains(int[] array, int key) { 
    Arrays.sort(array); 
    return Arrays.binarySearch(array, key) >= 0; 
    } 
    public static void println(Object line) { 
     System.out.println(line); 
    } 
    public static void main(String[] args) {   
     Scanner in = new Scanner(System.in); 

     String finalText = ""; 
     String[] input = new String[5]; 
     String[] swappedInput = new String[input.length]; 
     int[] usedIndex = new int[input.length]; 

     int swapCounter = input.length, useCounter; 

     for (int inputCounter = 0; inputCounter < input.length; inputCounter++) { //input 
      println("Enter input 1 " + (inputCounter + 1) + ": "); 
      input[inputCounter] = in.nextLine(); 
     } 

     while (--swapCounter > 0) { 
      do{ 
       useCounter = (int) Math.floor(Math.random() * input.length); 
      } 
      while (contains(usedIndex, useCounter)); 

      swappedInput[swapCounter] = input[swapCounter].concat("@" + input[useCounter]); 
      swappedInput[useCounter] = swappedInput[swapCounter].split("@")[0]; 
      swappedInput[swapCounter] = swappedInput[swapCounter].split("@")[1];  

      usedIndex[useCounter] = useCounter; 
     } 

     for (int outputCounter = 0; outputCounter < input.length; outputCounter++) { 
      finalText = finalText + swappedInput[outputCounter] + " "; 
     } 

     println("The swapped inputs are: " + finalText + "."); 
    } 
} 
+1

所有這些圖像鏈接都應作爲代碼格式文本發佈。請這樣做。 –

+0

'usedIndex'應該被'input'替代,也許在調用時包含方法?或者你沒有在'usedIndex'中定義值? – SMA

+0

因爲我沒有在'usedIndex'中定義值,所以輸入可以自由交換。 爲什麼要用'input'來替換'usedIndex','usedIndex'和'do-while'的用途是限制輸入重複自己,比如'alice bob bob bob eddy carol',而不是'alice bob dave eddy carol' –

回答

1

由於randomality某些倍的useCounter相同swapCounter現在看那些線(假定useCounter和swapCounter是相同的)

 swappedInput[swapCounter] = input[swapCounter].concat("@" + input[useCounter]); 
     swappedInput[useCounter] = swappedInput[swapCounter].split("@")[0]; 
     swappedInput[swapCounter] = swappedInput[swapCounter].split("@")[1]; 

在你正在改變XXX @ WWW的值是所述第二線WWW所以在第三行時做拆分你沒有得到一個數組有兩個值,你會得到一個空的結果這就是爲什麼異常拋出此外,你不應該使用swappedInput,因爲它擊敗pourpuse(如果我理解正確yoush shoud不使用臨時值而你使用的加法陣列更糟糕),正確的溶劑是隻使用輸入陣列這裏是解決方案

public class myFile { 
public static boolean contains(int[] array, int key) { 
    Arrays.sort(array); 
    return Arrays.binarySearch(array, key) >= 0; 
} 

public static void println(Object line) { 
    System.out.println(line); 
} 

public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 

    String finalText = ""; 
    String[] input = new String[5]; 
    int[] usedIndex = new int[input.length]; 

    int swapCounter = input.length, useCounter; 

    for (int inputCounter = 0; inputCounter < input.length; inputCounter++) { //input 
     println("Enter input 1 " + (inputCounter + 1) + ": "); 
     input[inputCounter] = in.nextLine(); 
    } 

    while (--swapCounter >= 0) { 
     do { 
      useCounter = (int) Math.floor(Math.random() * input.length); 
     } 
     while (contains(usedIndex, useCounter)); 

     // Skip if results are the same 
     if (useCounter == swapCounter) { 
      swapCounter++; 
      continue; 
     } 
     input[swapCounter] = input[swapCounter].concat("@" + input[useCounter]); 
     input[useCounter] = input[swapCounter].split("@")[0]; 
     input[swapCounter] = input[swapCounter].split("@")[1]; 

     usedIndex[useCounter] = useCounter; 
    } 

    for (int outputCounter = 0; outputCounter < input.length; outputCounter++) { 
     finalText = finalText + input[outputCounter] + " "; 
    } 

    println("The swapped inputs are: " + finalText + "."); 
} 

}

+0

它因爲useCounter是隨機選擇的 – urag

+0

但我仍然在第一個空' –

+0

因爲你應該從while(--swapCounter> 0)切換到while(--swapCounter> = 0)我改變它在回答 – urag