Java 初学者问题:虽然3x3数组里填入随机数的程式有问题,但可以执行,求改写.程式介绍:(字多和密密的,对不起.)我想产生100个随机数,数值范围必须1-9,顺序排列好1行,而且只显示最前3个的随机数

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 23:19:55
Java 初学者问题:虽然3x3数组里填入随机数的程式有问题,但可以执行,求改写.程式介绍:(字多和密密的,对不起.)我想产生100个随机数,数值范围必须1-9,顺序排列好1行,而且只显示最前3个的随机数

Java 初学者问题:虽然3x3数组里填入随机数的程式有问题,但可以执行,求改写.程式介绍:(字多和密密的,对不起.)我想产生100个随机数,数值范围必须1-9,顺序排列好1行,而且只显示最前3个的随机数
Java 初学者问题:虽然3x3数组里填入随机数的程式有问题,但可以执行,求改写.
程式介绍:(字多和密密的,对不起.)
我想产生100个随机数,数值范围必须1-9,顺序排列好1行,而且只显示最前3个的随机数给使用者.
然後,系统会询问使用者顺序为第1个的随机数,在3x3的数组里摆放的位置是怎样?
放入第1个随机数後,询问第2个随机数放进哪个位置,直至3x3数组都塞满满的.
要注意的是,3x3数组1个位置只能放置1个随机数.
而且决定1个随机数位置後,系统自动显示最新的3x3数组.
另外,当3x3数组塞满後,系统会提示使用者程式结束了,问Y/N问题 (卡在这里做不到!),
答Y,重新执行整个程式,重新一次游戏;
答N,系统会提示使用者Good Bye!,然後自动结束程式运行.
我写了5小时以上的程式码在下面,花费时间比同龄同学要好点,他们花了两个星期也搞不好
想到了,把最下面的statement放到while (point <99) 的下面,问题解决一大半了





Java 初学者问题:虽然3x3数组里填入随机数的程式有问题,但可以执行,求改写.程式介绍:(字多和密密的,对不起.)我想产生100个随机数,数值范围必须1-9,顺序排列好1行,而且只显示最前3个的随机数

你的代码逻辑基本是对的,但是结构很混乱,你应该把一个大问题拆分为很多小问题,分别解决,就会很清晰了,我按你的思路改了程序,但是你那个point我没弄明白,如果99次之后怎么办不是很明确,所以我干脆无视了

public class RandomInt {
private static int[] randomNumArray = null;
private static int[][] box = null;

// record how many numbers has been put into the box
private static int count;

private static void initRandomArray() {
randomNumArray = new int[100];
for (int i = 0; i < 100; i++) {
randomNumArray[i] = (int) (Math.random() * 9 + 1);
}
}

private static void initBox(Scanner scan) {
box = new int[3][3];
while (count < 9) {
System.out.println("You are choosing a location for the random number: " + randomNumArray[count]);
int row;
do {
System.out.println("Please input a row number: (from 0 to 2)");
row = scan.nextInt();
} while (!checkRowAvailable(row));
int column;
do {
System.out.println("Please input a column number: (from 0 to 2)");
column = scan.nextInt();
} while (!checkColumnAvailable(column));
if (checkPutAvailable(row, column)) {
box[row][column] = randomNumArray[count];
System.out.println("You have put a random number at the location of row: " + row + ", column: " + column);
printBox();
count++;
}
}
}

private static boolean checkRowAvailable(int row) {
if (row < 0 || row > 2) {
System.out.println("It should be a legal row number. (from 0 to 2)");
return false;
}
return true;
}

private static boolean checkColumnAvailable(int column) {
if (column < 0 || column > 2) {
System.out.println("It should be a legal column number. (from 0 to 2)");
return false;
}
return true;
}

private static boolean checkPutAvailable(int row, int column) {
if (box[row][column] != 0) {
System.out.println("The location you chose has been occupied already, please ipnut again.");
return false;
}
return true;
}

private static void printBox() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(box[i][j]);
if (j < 2) {
System.out.print(" | ");
}
}
System.out.println();
}
}

private static void playGame(Scanner scan) {
count = 0;
initRandomArray();
initBox(scan);
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
playGame(scan);
while (true) {
System.out.println("\nDo you want to continue? (y/n)");
String s = scan.next();
if ("y".equalsIgnoreCase(s)) {
System.out.println();
playGame(scan);
} else if ("n".equalsIgnoreCase(s)) {
System.out.println("Good Bye!");
break;
}
}
scan.close();
}
}