这算是个小游戏
猜随机数
代码如下
package Day07; import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class MatchingGame {
public static void main(String[] args) {
int[] arrOne = new int[5];
for(int i = 0; i < arrOne.length; i++) {
arrOne[i] = new Random().nextInt(10);
for(int j = 0; j < i; j++) {
if (arrOne[i] == arrOne[j]) {
j = 0;
arrOne[i] = new Random().nextInt(10);
}
}
}
int[] arrTwo = new int[5];
Scanner sc = new Scanner(System.in);
String str;
// System.out.println(Arrays.toString(arrOne));
boolean loopFlag = true;
while (loopFlag) {
for (int i = 0; i < arrOne.length; i++) {
System.out.println("Please input arrTwo[" + i + "]");
arrTwo[i] = sc.nextInt();
}
str = match(arrOne, arrTwo);
System.out.println(str);
loopFlag = !"a=5,b=0".equals(str);
}
}
/**
*
* @param arrOne 源数组
* @param arrTwo 与源数组匹配的数组
* @return
*/
public static String match(int[] arrOne, int[] arrTwo) {
int a = 0;
int b = 0;
for (int i = 0; i < arrOne.length; i++) {
for (int j = 0; j < arrTwo.length; j++) {
if (i == j && arrOne[i] == arrTwo[j]) {
a++;
} else if (i != j && arrOne[i] == arrTwo[j]) {
b++;
}
}
}
return "a=" + a + ",b=" + b;
}
}