Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我做了一个for循环来交换字符串中的两个indivchar变量_Java_String_Random_Char_Swap - Fatal编程技术网

Java 我做了一个for循环来交换字符串中的两个indivchar变量

Java 我做了一个for循环来交换字符串中的两个indivchar变量,java,string,random,char,swap,Java,String,Random,Char,Swap,我应该写一种对一个单词进行置乱的方法,在一个不是第一个或最后一个字符的单词中切换两个字母 我已经将I和j初始化为位置1和str.length()-1之间的随机整数。这个循环为什么不打印单词的加扰版本 char[] chararray = word.toCharArray(); int i = (int) (Math.random() * ((word.length()-2) - (1)) + 1); int j = (int) (Math.random() * ((word.length()-

我应该写一种对一个单词进行置乱的方法,在一个不是第一个或最后一个字符的单词中切换两个字母

我已经将I和j初始化为位置1和str.length()-1之间的随机整数。这个循环为什么不打印单词的加扰版本

char[] chararray = word.toCharArray();
int i = (int) (Math.random() * ((word.length()-2) - (1)) + 1); 
int j = (int) (Math.random() * ((word.length()-2) - (1)) + 1); 
for (int x = 0; x < word.length(); x++) 
    {
        if (x == i) 
        {
             chararray[x] = word.charAt(i);
        }
        else if (x == j)
        {
            chararray[x] = word.charAt(j);
        }
    }

  word = new String (chararray);
  System.out.println(word);
char[]chararray=word.toCharArray();
int i=(int)(Math.random()*((word.length()-2)-(1))+1);
int j=(int)(Math.random()*((word.length()-2)-(1))+1);
对于(int x=0;x

当我输入
tofu
时,它会重新打印
tofu
。我想输入“tofu”并让它输出“tfou”。

我想您会寻找以下内容

 for (int x = 0; x < word.length(); x++) 
    {
        if (x == i) 
        {
             chararray[x] = word.charAt(j);
        }
        else if (x == j)
        {
            chararray[x] = word.charAt(i);
        }
    }
for(int x=0;x
交换if和else块中的i和j值

当x等于i时,您将在字符中重新分配相同的值,因此您将获得相同的值。但你的意图是在我希望的i和j位置交换角色


还有一件事,确保我从不等于j

从字符数组开始。
使用随机生成器获取介于0(包含)和字符数组长度(排除)之间的2个整数。

如果
i!=j
,交换它们。(无需将字符与自身交换)

结果:

Going to swap t at position 7 with e at position 2

Result: eltphane
更新:

Going to swap t at position 7 with e at position 2

Result: eltphane

charArr.length
更改为
charArr.length-2+1
以排除第一个和最后一个字符。

您的逻辑似乎是错误的

 if (x == i) 
        {
             chararray[x] = word.charAt(i);
        }
上面的内容将指向相同的索引,x和i都指向相同的索引,您需要交换索引

以下代码

public static void main(String args[]) {
        String word = "ankur";
        char[] chararray = word.toCharArray();
        int i = (int) (Math.random() * ((word.length() - 2) - (1)) + 1);
        int j = (int) (Math.random() * ((word.length() - 2) - (1)) + 1);
        System.out.println(i + " and " + j);
        for (int x = 0 ; x < word.length() ; x++) {
            if (x == i) {
                chararray[x] = word.charAt(j);
            } else if (x == j) {
                chararray[x] = word.charAt(i);
            }
        }

        word = new String(chararray);
        System.out.println(word);
    }

它打印什么?你能告诉我们完整的代码吗?想知道如何初始化chararray,我和jI认为您需要向我们展示输入字符串,以及实际和预期的输出。@Gosu如果i==j,您的意思是什么?公共静态字符串加扰(string-word){char[]chararray=word.toCharArray()int i=(int)(Math.random()*((word.length()-2)-(1))+1);intj=(int)(Math.random()*((word.length()-2)-(1))+1);for(intx=0;x2 and 1 aknur