我已经尝试了一段时间不同的事情,我无法弄清楚为什么我的逻辑错了。这没有意义。
我正在尝试根据以下伪代码制作程序。
翻译以下伪代码,以便将字符串中的字符随机置换为Java程序。
first + word.charAt(j) + middle + word.charAt(i) + last
这是我到目前为止所做的:
package assignment4;
import java.util.Scanner;
public class P4Point7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter a word: ");
String word = in.next();
in.close();
int wordLength = word.length(); // Gets the word.Length
int x = 1; // Primes the loop
while (x <= wordLength) {
int i = (int) ((Math.random() * wordLength) - 1); // Gets a random letter i that is not the last letter
int j = (int) (Math.random() * (wordLength - i)) + i; // Gets a random letter j after i
String first = word.substring(0, i); // Gets first part of word
String middle = word.substring(i + 1, j); // Gets middle part of word
String last = word.substring(j + 1, wordLength); // Gets last part of word
x++; // Increments the loop
String status = first + word.charAt(j) + middle + word.charAt(i) + last; // Swaps i and j in the word
System.out.println(status);
}
}
}
我遇到的问题是
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at assignment4.P4Point7.main(P4Point7.java:21)
我正在用“Waffle”这个词测试程序。
答案 0 :(得分:1)
一些建议:
使用Random
对象生成随机索引可以简化索引生成。 Math.random()
返回介于0.0和1.0之间的双精度。将其乘以字长不能保证返回0到wordlength - 1;
Random indexGen = new Random();
int i = indexGen.nextInt(wordlength -1);
int j = indexGen.nextInt(wordlength -1 - i) + i;
另外,我读取伪代码的方式是你只想将i处的字符与j处的字符交换,这样你就可以这样做
char[] charArray = yourWord.toCharArray()
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
String finalWord = new String(charArray);
使用子字符串编辑
首先修复您的逻辑,以便您的索引始终正确并确保0 <= i < j <= wordlength
。
把它放入if语句然后你会知道索引是否错误
if( 0 <= i && i < j && j <=wordlength)
{
String first = word.substring(0, i);
String middle = word.substring(i+1, j);
String last = word.substring(j+1, wordLength);
//... etc
}
else
System.out.println("There was an indexing error: i = " + i + ", j = " + j);
编辑索引
int i = (int) ((Math.random() * wordLength) - 1)
if ( i < 0)
i = 0;
然后对j同样但检查j > i
。另一种选择是使用Math.abs()
,如此
int i = Math.abs((int) ((Math.random() * wordLength) - 1))
答案 1 :(得分:1)
i = (int) ((Math.random() * wordLength) - 1);
j = (int) (Math.random() * (wordLength - i)) + i; //
在这两行中,有(int) (Math.random() * (wordLength - i))
个案例会导致0
和i == j
。如果是,那么,以下代码行:
String middle = word.substring(i+1, j);
// if i==j, then i+1 > j which will result in index exception.
random.nextInt(n)
的{{3}}类来返回0(包括)和指定值(不包括)之间的随机整数。答案 2 :(得分:0)
在我看来,你的i和j可以是字符串大小之外的位置。 尝试改变你获得随机位置的方式。
尝试:
Random rand = new Random();
int i = rand.nextInt(wordLength - 1); // This will return you a random position between 0 and wordLength -2
int j = rand.nextInt(wordLength - i) + i; // This should give you a position after or equals i.
答案 3 :(得分:0)
就像上面提到的Sage和Java Devil一样,第18-19行导致了你的问题,但是由于你使用的是Math.random()
,在调试器中重现问题会很困难 - 你正在创造一些东西这本质上是不确定的。但是,既然你正在寻找随机,那么关键是要确保
i
&gt; = 0,j
&gt; i
(或i+1
,具体取决于您想要的确切结果)。 这个练习似乎正在探索Math
库,因此我建议您研究一些其他方法,以确保i
和j
始终具有可接受的值(例如, Math.min()
和Math.max()
)。
答案 4 :(得分:0)
这是解决方案。
import java.util.Scanner;
public class PermuteCharacters
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a word to permute: ");
String word = in.next();
for (int n = 0; n < word.length(); n++)
{
/**
The -1 here guarantees that later we won't pick a j that goes off
the end of the string. This is important since since the
pseudocode tells us to pick a j > i
*/
int i = (int) (Math.random() * word.length() - 1);
int j = (int) (Math.random() * (word.length() - i - 1)) + i + 1;
String first = word.substring(0, i);
String middle = word.substring(i + 1, j);
String last = word.substring(j + 1);
word = first + word.charAt(j) + middle + word.charAt(i) + last;
}
System.out.println("Your permuted word is: " + word);
}
}
答案 5 :(得分:0)
这段代码对我有用:
import java.util.Random;
public class Word
{
private Random generator = new Random();
public Word()
{
generator = new Random();
final long SEED = 42;
generator.setSeed(SEED);
}
public String scramble(String word)
{
int lenWord = word.length();
for (int l = 0; l < lenWord; l++)
{
int i = generator.nextInt(lenWord - 1);
int j = i + 1 + generator.nextInt(lenWord - i - 1);
String letterI = word.substring(i, i + 1);
String letterJ = word.substring(j, j + 1);
String FIRST = word.substring(0, i);
String MIDDLE = word.substring(i + 1, j);;
String LAST = word.substring(j + 1, lenWord);
word = FIRST + letterJ + MIDDLE + letterI + LAST;
}
return word;
}
}