这个Java交换程序有什么问题,为什么以及我该怎么办?

时间:2017-09-03 14:11:28

标签: java

我在不使用 temp 变量的情况下进行多字符串输入随机交换。

但是当我输入时,会发生几次:

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 :(得分: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(如果我理解正确你在使用额外数组时不要使用临时值,这是更糟糕的)正确的解决方法是只使用输入数组这里是解决方案

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 + ".");
}

}