Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 我必须在数组中取整数并以随机顺序输出它们_Java_Arrays - Fatal编程技术网

Java 我必须在数组中取整数并以随机顺序输出它们

Java 我必须在数组中取整数并以随机顺序输出它们,java,arrays,Java,Arrays,这是我需要的输出( 输入阵列:1234567 随机输出:7236154) 这就是我得到的 数组的输入大小 五, 输入值 一, 输入值 二, 输入值 三, 输入值 四, 输入值 五, 随机输出:2 随机输出:0 随机输出:0 随机输出:0 随机输出:0 问题出在第23行,我不知道怎么解决 import java.util.Random; import java.util.Scanner; public class problem_2 { public st

这是我需要的输出( 输入阵列:1234567 随机输出:7236154)

这就是我得到的

数组的输入大小

五,

输入值

一,

输入值

二,

输入值

三,

输入值

四,

输入值

五,

随机输出:2

随机输出:0

随机输出:0

随机输出:0

随机输出:0

问题出在第23行,我不知道怎么解决

    import java.util.Random;
    import java.util.Scanner;

    public class problem_2 {

       public static void main(String args[]){
       Random r = new Random();
       Scanner m = new Scanner(System.in);      
       System.out.println("Input size of the Array");   
       int size = m.nextInt();
       int a[] = new int[size];
       int b[] = new int[size];

        for(int i = 0;i<a.length;i++) {         
           System.out.println("Input Value " +(i+1));
           a[i] = m.nextInt();
          }
       int cell = 0;
       int x = r.nextInt(size);
       int value = a[x];
       while(cell<size) {

        for(int i =0; i<= size;i++) {
            if (b[i]==value) {
                cell++;
            }

            if(cell==0) {
                b[cell] = value;
                cell++;
            }
            System.out.println("Random Output: "+b[i]);
            }
          } 
        }
      }
import java.util.Random;
导入java.util.Scanner;
公共课问题2{
公共静态void main(字符串参数[]){
随机r=新随机();
扫描器m=新扫描器(System.in);
System.out.println(“数组的输入大小”);
int size=m.nextInt();
int a[]=新的int[size];
int b[]=新的int[size];

对于(int i=0;i而言,问题在于您的代码在以下for循环中的索引过多:

for(int i =0; i<= size;i++)

for(int i=0;我感谢大家的帮助,但我没有从其他人那里学到任何东西,所以
我找到了一个更简单的方法

import java.util.Random;
import java.util.Scanner;
public class random{
public static void main(String args[]){
    Random r = new Random();
    Scanner m = new Scanner(System.in);     
    System.out.println("Input size of the Array");  
    int size = m.nextInt();
    int a[] = new int[size];
    int b[] = new int[size];

    for(int i = 0;i<a.length;i++) {         
        System.out.println("Input Value ");
        a[i] = m.nextInt();
    }
    int cell = 0;
    while(cell<size) {
        int n = r.nextInt(size);
        int value = a[n];
        int count = 0;
        for(int i =0; i< size;i++) {
            if (b[i]== value) {
                count++;
            }
        }
        if(count==0) {
            b[cell] = value;
            cell++;
        }

    } 
    System.out.println ("\n");
    System.out.println("Input Array: ");
    for (int i = 0; i<size; i++){      
        System.out.print(a[i] + " ");
    }    
    System.out.println ("\n");
    System.out.println("Random Output: ");
    for (int i = 0; i<size; i++){      
        System.out.print(b[i] + " ");
    }                                       
}
import java.util.Random;
导入java.util.Scanner;
公共类随机{
公共静态void main(字符串参数[]){
随机r=新随机();
扫描器m=新扫描器(System.in);
System.out.println(“数组的输入大小”);
int size=m.nextInt();
int a[]=新的int[size];
int b[]=新的int[size];

对于(int i=0;i您的问题是什么?我否决了这个非问题,因为它不是一个问题。我如何修复它?没有证据表明对此代码执行了任何调试。请您的问题向我们展示您的调试发现了什么,以及关于特定代码行的特定问题。请参阅:和。您可以这样做by将整数数组元素放入整数,然后对该ArrayList对象使用该方法,最后将ArrayList中的元素推回原始整数数组。
        //Randomize the array
    for(int i = 0; i < size;i++) {
            //lets get a random index in the array
            int randIndex = (int)(Math.random()*size);
            //then we will store the index we are swapping because its going to be overwritten
            int temp = a[i];
            //set our index equal to the random one
            a[i] = a[randIndex];
            //put our index's original value into the random index, so its not lost
            a[randIndex] = temp;
    }
import java.util.Random;
import java.util.Scanner;
public class random{
public static void main(String args[]){
    Random r = new Random();
    Scanner m = new Scanner(System.in);     
    System.out.println("Input size of the Array");  
    int size = m.nextInt();
    int a[] = new int[size];
    int b[] = new int[size];

    for(int i = 0;i<a.length;i++) {         
        System.out.println("Input Value ");
        a[i] = m.nextInt();
    }
    int cell = 0;
    while(cell<size) {
        int n = r.nextInt(size);
        int value = a[n];
        int count = 0;
        for(int i =0; i< size;i++) {
            if (b[i]== value) {
                count++;
            }
        }
        if(count==0) {
            b[cell] = value;
            cell++;
        }

    } 
    System.out.println ("\n");
    System.out.println("Input Array: ");
    for (int i = 0; i<size; i++){      
        System.out.print(a[i] + " ");
    }    
    System.out.println ("\n");
    System.out.println("Random Output: ");
    for (int i = 0; i<size; i++){      
        System.out.print(b[i] + " ");
    }                                       
}