Java 如何将我的私有int转换为公共int
我想多次使用Math.random对数组进行置乱,但我不知道如何将随机整数放入置乱中并多次使用随机整数Java 如何将我的私有int转换为公共int,java,private,public,Java,Private,Public,我想多次使用Math.random对数组进行置乱,但我不知道如何将随机整数放入置乱中并多次使用随机整数 public static void scramble(int[] array){ for(int i = 0 ; i < array.length - 1; i++){ int temp = array[i]; array[i] = array[random]; array[random] = temp;}} public int random
public static void scramble(int[] array){
for(int i = 0 ; i < array.length - 1; i++){
int temp = array[i];
array[i] = array[random];
array[random] = temp;}}
public int random (){
return (int)(Math.random() *9) + 1;}
全司机
import java.lang.Math;
public class Driver03{
public static void main(String[] args){
int[] array = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
print(array);
scramble(array);
print(array);
print(array);}
public static void print(int[] array){
for(int x = 0; x < array.length; x++){
System.out.print(" " + array[x]);}
System.out.println("");}
public static void scramble(int[] array){
int random = random();
for(int i = 0 ; i < array.length - 1; i++){
int temp = array[i];
array[i] = array[random];
array[random] = temp;}}
public int random (){
return (int)(Math.random() *9) + 1;}
}
import java.lang.Math;
公共类驱动程序03{
公共静态void main(字符串[]args){
int[]数组={100、101、102、103、104、105、106、107、108、109};
打印(阵列);
置乱(阵列);
打印(阵列);
打印(数组);}
公共静态无效打印(int[]数组){
对于(int x=0;x
首先,必须像“random()”那样调用random函数,而不仅仅是random函数
请尝试以下代码:
public static void scramble(int[] array){
int random = random();
for(int i = 0 ; i < array.length - 1; i++){
int temp = array[i];
array[i] = array[random];
array[random] = temp;}}
public static int random (){
return (int)(Math.random() *9) + 1;}
公共静态无效置乱(int[]数组){
int random=random();
for(int i=0;i
下面是一个使用的实现
publicstaticvoidmain(字符串[]args)
{
int[]值=新的int[]{100、101、102、103、104、105、106、107、108、109};
System.out.println(“开始:“+Arrays.toString(value));
置乱(值);
System.out.println(“加扰:“+Arrays.toString(value));
数组。排序(值);
System.out.println(“排序:”+Arrays.toString(value));
}
公共静态无效置乱(int[]数组)
{
//使用Fisher-Yates shuffle进行攀爬。
随机rnd=新随机();
for(int i=0;i
它不使用
Math.random()
,而是使用random
的一个实例。你能解释得更好吗?有一个输入和预期输出的示例也是很有用的。需要注意的一点是,random()函数返回的值介于1和9之间,如果传入scramble()函数的数组长度不超过10个元素,则可能会收到索引超出范围的异常。您可能希望更新random()函数以接受数组中要加扰的元素数的参数。我确实尝试过此操作,但出现了“无法从静态上下文引用非静态方法random()”错误,请将“static”添加到“random”:@ChaseDiaz,我更新了代码。我想这会解决你的问题
public static void scramble(int[] array){
int random = random();
for(int i = 0 ; i < array.length - 1; i++){
int temp = array[i];
array[i] = array[random];
array[random] = temp;}}
public static int random (){
return (int)(Math.random() *9) + 1;}
public static void main( String[] args )
{
int[] values = new int[] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109 };
System.out.println( "Start: " + Arrays.toString( values ) );
scramble( values );
System.out.println( "Scrambled: " + Arrays.toString( values ) );
Arrays.sort( values );
System.out.println( "Sorted: " + Arrays.toString( values ) );
}
public static void scramble( int[] array )
{
// Scramble using the Fisher-Yates shuffle.
Random rnd = new Random();
for ( int i = 0; i < array.length - 1; i++ )
{
int random = i + rnd.nextInt( array.length - 1 - i );
int temp = array[ random ];
array[ random ] = array[ i ];
array[ i ] = temp;
}
}