Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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中的math.random使用另一个数组中的字符填充数组_Java_Arrays_Math_Random_Char - Fatal编程技术网

如何使用JAVA中的math.random使用另一个数组中的字符填充数组

如何使用JAVA中的math.random使用另一个数组中的字符填充数组,java,arrays,math,random,char,Java,Arrays,Math,Random,Char,我试图从一个包含七个字符的数组中,用四个随机字符填充一个新数组。这可能吗?我似乎找不到正确的方法 这是我尝试过的,但我犯了一个错误 char charactersAllowed[] = {'a','b','c','d','e','f','g'}; char currentCharacters[] = charactersAllowed[ (int) (Math.random() * 4) ]; 错误:不兼容类型:无法将字符转换为字符[]这是因为您正在访问数组中的单个字符 通过简单循环获得4个

我试图从一个包含七个字符的数组中,用四个随机字符填充一个新数组。这可能吗?我似乎找不到正确的方法

这是我尝试过的,但我犯了一个错误

char charactersAllowed[] = {'a','b','c','d','e','f','g'};
char currentCharacters[] = charactersAllowed[ (int) 
(Math.random() * 4) ];

错误:不兼容类型:无法将字符转换为字符[]

这是因为您正在访问数组中的单个字符

通过简单循环获得4个随机字符:

char[]currentCharacters=新字符[4];
对于(int i=0;i

还要注意,最好将数组定义为
Object[]variable
而不是
Object variable[]

您使用的是哪种语言?关于标签还不清楚。对不起,我用的是Java。谢谢。如果我使用for循环,我想我说的是正确的,我需要将代码放在一个方法中。如何将值返回到currentCharacters数组?