[英]I have to take integers in an array and output them in random order
This is the output i need ( Input Array: 1 2 3 4 5 6 7 Random Output: 7 2 3 6 1 5 4)这是我需要的输出(输入数组:1 2 3 4 5 6 7 随机输出:7 2 3 6 1 5 4)
this is what i get这就是我得到的
Input size of the Array数组的输入大小
5 5
Input Value输入值
1 1
Input Value输入值
2 2
Input Value输入值
3 3
Input Value输入值
4 4
Input Value输入值
5 5
Random Output: 2随机输出:2
Random Output: 0随机输出:0
Random Output: 0随机输出:0
Random Output: 0随机输出:0
Random Output: 0随机输出:0
The problem is with line 23 and im not sure how to fix it问题出在第 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]);
}
}
}
}
The problem is your code is going one too many indexes in the following for loop:问题是您的代码在以下 for 循环中使用了太多索引:
for(int i =0; i<= size;i++)
That's because you have to remember an array with say 5 elements has indexes 0-4.那是因为您必须记住一个包含 5 个元素的数组,其索引为 0-4。 So while the size is the number of elements in the array the largest index is always (the number of elements) - 1.
因此,虽然大小是数组中元素的数量,但最大的索引始终是(元素数量)- 1。
So you should write the for loop like so:所以你应该像这样编写 for 循环:
for(int i = 0; i < size;i++)
Even then your code doesn't quite randomize correctly.即使这样,您的代码也不会完全正确随机化。 The easiest way to randomize an array would be to swap out each element with another random element, like this:
随机化数组的最简单方法是用另一个随机元素交换每个元素,如下所示:
//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;
}
thanks everyone for the help but i didnt learn any of the things in the others so感谢大家的帮助,但我没有学到其他人的任何东西,所以
i found a easier way to do it我找到了一种更简单的方法
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] + " ");
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.