如何创建不相等的随机整型常量
假设我要创建各含9个整型常量的数组 a、b、c 和 d,其中a[1],b[0]都是1~9的随机数。
并且数组中其他整型常量与 a[1] , b[0] 的关系如下:
a[0] = a[1] + 2;
a[2] = a[1] + 1;
a[3] = a[1] + 2;
a[4] = a[1] + 3;
a[5] = a[1] + 4;
a[6] = a[1] + 2;
a[7] = a[1] + 1;
a[8] = a[1] + 2;
a[9] = a[1] + 3;
b[1] = b[0] + 1;
b[2] = b[0] + 1;
b[3] = b[0] + 1;
b[4] = b[0] + 1;
b[5] = b[0] + 1;
b[6] = b[0] + 2;
b[7] = b[0] + 3;
b[8] = b[0] + 3;
b[9] = b[0] + 3;
c[0] = a[0] * 10 + b[0];
c[1] = a[1] * 10 + b[1];
c[2] = a[2] * 10 + b[2];
c[3] = a[3] * 10 + b[3];
c[4] = a[4] * 10 + b[4];
c[5] = a[5] * 10 + b[5];
c[6] = a[6] * 10 + b[6];
c[7] = a[7] * 10 + b[7];
c[8] = a[8] * 10 + b[8];
c[9] = a[9] * 10 + b[9];
d[0] = b[0] * 10 + a[0];
d[1] = b[1] * 10 + a[1];
d[2] = b[2] * 10 + a[2];
d[3] = b[3] * 10 + a[3];
d[4] = b[4] * 10 + a[4];
d[5] = b[5] * 10 + a[5];
d[6] = b[6] * 10 + a[6];
d[7] = b[7] * 10 + a[7];
d[8] = b[8] * 10 + a[8];
d[9] = b[9] * 10 + a[9];
那么我要如何写这个程序才能使数组 c 和数组 d 中的每一个常量的值都不相等。
多谢各位大虾!!
----------------解决方案--------------------------------------------------------
程序代码:
import java.util.*;
public class TestRandom
{
public static void main(String[] args) {
Random ran = new Random();
int a[] = new int[10];
int b[] = new int[10];
int c[] = new int[10];
int d[] = new int[10];
boolean flag;
do
{
flag = false;
a[1] = ran.nextInt(9)+1;
b[0] = ran.nextInt(9)+1;
a[0] = a[1] + 2;
a[2] = a[1] + 1;
a[3] = a[1] + 2;
a[4] = a[1] + 3;
a[5] = a[1] + 4;
a[6] = a[1] + 2;
a[7] = a[1] + 1;
a[8] = a[1] + 2;
a[9] = a[1] + 3;
b[1] = b[0] + 1;
b[2] = b[0] + 1;
b[3] = b[0] + 1;
b[4] = b[0] + 1;
b[5] = b[0] + 1;
b[6] = b[0] + 2;
b[7] = b[0] + 3;
b[8] = b[0] + 3;
b[9] = b[0] + 3;
c[0] = a[0] * 10 + b[0];
c[1] = a[1] * 10 + b[1];
c[2] = a[2] * 10 + b[2];
c[3] = a[3] * 10 + b[3];
c[4] = a[4] * 10 + b[4];
c[5] = a[5] * 10 + b[5];
c[6] = a[6] * 10 + b[6];
c[7] = a[7] * 10 + b[7];
c[8] = a[8] * 10 + b[8];
c[9] = a[9] * 10 + b[9];
d[0] = b[0] * 10 + a[0];
d[1] = b[1] * 10 + a[1];
d[2] = b[2] * 10 + a[2];
d[3] = b[3] * 10 + a[3];
d[4] = b[4] * 10 + a[4];
d[5] = b[5] * 10 + a[5];
d[6] = b[6] * 10 + a[6];
d[7] = b[7] * 10 + a[7];
d[8] = b[8] * 10 + a[8];
d[9] = b[9] * 10 + a[9];
for (int x : c)
{
for (int y : d )
if (x==y)
{
flag = true;
}
}
} while (flag);
System.out.println("---a---");
for (int x : a)
{
System.out.println(x);
}
System.out.println("---b---");
for (int x : b )
{
System.out.println(x);
}
System.out.println("---c---");
for (int x : c )
{
System.out.println(x);
}
System.out.println("---d---");
for (int x : d )
{
System.out.println(x);
}
}
}
public class TestRandom
{
public static void main(String[] args) {
Random ran = new Random();
int a[] = new int[10];
int b[] = new int[10];
int c[] = new int[10];
int d[] = new int[10];
boolean flag;
do
{
flag = false;
a[1] = ran.nextInt(9)+1;
b[0] = ran.nextInt(9)+1;
a[0] = a[1] + 2;
a[2] = a[1] + 1;
a[3] = a[1] + 2;
a[4] = a[1] + 3;
a[5] = a[1] + 4;
a[6] = a[1] + 2;
a[7] = a[1] + 1;
a[8] = a[1] + 2;
a[9] = a[1] + 3;
b[1] = b[0] + 1;
b[2] = b[0] + 1;
b[3] = b[0] + 1;
b[4] = b[0] + 1;
b[5] = b[0] + 1;
b[6] = b[0] + 2;
b[7] = b[0] + 3;
b[8] = b[0] + 3;
b[9] = b[0] + 3;
c[0] = a[0] * 10 + b[0];
c[1] = a[1] * 10 + b[1];
c[2] = a[2] * 10 + b[2];
c[3] = a[3] * 10 + b[3];
c[4] = a[4] * 10 + b[4];
c[5] = a[5] * 10 + b[5];
c[6] = a[6] * 10 + b[6];
c[7] = a[7] * 10 + b[7];
c[8] = a[8] * 10 + b[8];
c[9] = a[9] * 10 + b[9];
d[0] = b[0] * 10 + a[0];
d[1] = b[1] * 10 + a[1];
d[2] = b[2] * 10 + a[2];
d[3] = b[3] * 10 + a[3];
d[4] = b[4] * 10 + a[4];
d[5] = b[5] * 10 + a[5];
d[6] = b[6] * 10 + a[6];
d[7] = b[7] * 10 + a[7];
d[8] = b[8] * 10 + a[8];
d[9] = b[9] * 10 + a[9];
for (int x : c)
{
for (int y : d )
if (x==y)
{
flag = true;
}
}
} while (flag);
System.out.println("---a---");
for (int x : a)
{
System.out.println(x);
}
System.out.println("---b---");
for (int x : b )
{
System.out.println(x);
}
System.out.println("---c---");
for (int x : c )
{
System.out.println(x);
}
System.out.println("---d---");
for (int x : d )
{
System.out.println(x);
}
}
}
----------------解决方案--------------------------------------------------------
感谢2楼,不过能解释一下您的for循环吗?
我是个初学者,对for循环不太了解。
----------------解决方案--------------------------------------------------------
以下是引用MoonVer在2010-8-4 19:17:03的发言:
感谢2楼,不过能解释一下您的for循环吗?
我是个初学者,对for循环不太了解。
java中foreach语法感谢2楼,不过能解释一下您的for循环吗?
我是个初学者,对for循环不太了解。
格式:
for(元素类型type 元素变量x : 遍历对象obj)
{ 引用了x的java语句; }
Example:
public class Test {
public static void main(String[] args)
{
int[] a = {1,2,3};
for(int i : a)
System.out.print(i + " ");
}
}
[ 本帖最后由 lampeter123 于 2010-8-4 19:52 编辑 ]
----------------解决方案--------------------------------------------------------