public class WordScrambleEx1 {
public static void main(String[] args) {
String[] strArr = {"CHANGE", "LOVE", "HOPE", "VIEW"};
String answer = getAnswer(strArr);
String question = getScrambledWord(answer);
System.out.println("Question :" + question);
System.out.println("Answer: " + answer);
}
public static String getAnswer(String[] strArr) {
String i = strArr[(int)Math.random()*4];
return i;
}
public static String getScrambledWord(String str) {
char[] character = str.toCharArray();
String question1 = null;
for(int i = 0; i < character.length; i ++)
{
char[] java = new char [(int)Math.random()*i] ;
question1 = new String(java);
}
return question1;
}
}
我对Java很陌生并且给出了一个问题,我给出了四个字母,我的方法需要使用Math.random随机选择其中一个并加扰该字符串的字符。
我的代码从给定数组中找到一个String,但不对字符串进行加扰。谁能告诉我我做错了什么?
理解构造函数和范围真的很难。
答案 0 :(得分:0)
第一个错误:
(int) Math.random() * i
将始终返回0,因为Math.random()返回一个介于0和1之间的浮点数,因此当它转换为int时它总是为零(int不圆,它只是截断逗号后面的数字)。
您可以使用以下方法解决此问题:
(int) (Math.random() * i)
现在我们首先将Math.random()的float结果与i相乘,得到一个float,因为第一个数字是float。然后我们将这个float转换为int。
第二个错误:public static String getScrambledWord(String str) {
char[] character = str.toCharArray();
String question1 = null;
for(int i = 0; i < character.length; i ++)
{
char[] java = new char [(int)Math.random()*i] ;
question1 = new String(java);
}
return question1;
}
每次迭代都会创建一个长度为0的新char数组,然后将question1设置为它,它总是一个空字符串,因为java数组中没有任何内容。
我会这样做:
public static String getScrambledWord(String str) {
char[] character = str.toCharArray();
String question1 = new String();
ArrayList<Character> chars = new ArrayList<Character>(); //an arraylist is an array wich dynamically changes its size depending on the amount of its elements
for (int i = 0; i < character.length; i++) {// first we put all characters of the word into that arraylist
chars.add(character[i]);
}
while(chars.size()>0){//then we iterate over the arraylist as long as it has more than 0 elements
int index = (int)(Math.random() * chars.size());//we create a random index in the range of 0 and the arraylists size
question1 += chars.get(index);// we add the letter at the index we generated to the scrambled word variable
chars.remove(index);// then we remove the character we just added to the scrambled word, from the arraylist, so it cant be in there twice
}// thus the size decreases by 1 each iteration until every element of the arrraylist is somewhere in the scrambled word
return question1;
}
答案 1 :(得分:0)
您的代码中存在一些错误。生成随机整数的方式具有误导性。让我们看一下(int)Math.random() * 4
语句的解释。 Math.random()确实:
返回带有正号的double值,大于或等于0.0且小于1.0。
现在,在Java中,类型转换优先于+
,-
,*
和/
,因此实际发生的是((int)Math.random()) * 4
。 Math.random()
会在0.0
和1.0
独占之间返回一个浮点数,因此大致为[0.0,0.999999 ...]。对int
的强制转换将截断所有小数位,您将始终获得0
。然后,您的陈述将简化为0 * 4 = 0
。总的来说,你总是得到第一个字。
我建议你改用Random
课程。它提供了一种方法nextInt(int n)
,它返回0
包含和n
独占之间的随机整数,因此[0, n - 1]
由于代码中存在很多错误,我想为您提供此解决方案:
import java.util.Random;
public class WordScrambleEx1 {
private static Random random;
public static void main(String[] args) {
// Create object of class (initializes the
// random generator with a default seed)
random = new Random();
String[] strArr = { "CHANGE", "LOVE", "HOPE", "VIEW" };
String answer = getAnswer(strArr);
String question = getScrambledWord(answer);
System.out.println("Question: " + question);
System.out.println("Answer: " + answer);
}
public static String getAnswer(String[] strArr) {
// Chooses a random index in [0, strArr.length - 1]
int index = random.nextInt(strArr.length);
String i = strArr[index];
return i;
}
public static String getScrambledWord(String str) {
String remaining = str;
String scrambled = "";
// Loop over the string, each time choose a random letter
// and add it to the scrambled word, then remove that letter
// from the remaining word. Repeat until all letters are gone.
for (int i = str.length(); i > 0; i--) {
// Choose the index of a letter in the remaining string
int index = random.nextInt(remaining.length());
// Add the letter at the random index to your scambled word
scrambled += remaining.charAt(index);
// Remove the chosen character from the remaining sequence
remaining = remaining.substring(0, index) + remaining.substring(index + 1);
}
return scrambled;
}
}