gpt4 book ai didi

java - 在执行操作之前检查数组的内容

转载 作者:行者123 更新时间:2023-11-30 07:34:22 25 4
gpt4 key购买 nike

该程序的目的是构建一个由句点(.)组成的 10x10 数组网格,允许用户指定一个起点,然后程序将随机选择分配给“步行者”方向的数字去。每次移动时,它都会用字母表中的下一个字母标记其位置(起点用 A 标记)。如果步行者越出数组的边界(AKA > 10 或 < 0),它会说“你被捕了”,如果变量 alpha == 'Z' 它会说“你已经回家了”。

我剩下要做的唯一一件事是,如果步行者试图返回到它已经去过的地方,它会跳到下一个空间,直到到达它没有去过的空间。

package walktester;

import java.lang.Math;
import java.util.Random;
import java.util.Scanner;

class DrunkWalker {
private char[][] walkgrid = new char[10][10];
private static int randNSEW;
private int randomnum;
private int startrow;
private int startcol;
private char alpha = 'A';
private int nextrow;
private int nextcol;

public DrunkWalker(int r, int c) {
startrow = r;
startcol = c;
nextrow = startrow;
nextcol = startcol;

for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j++)
walkgrid[i][j] = '.';
}
walkgrid[r][c] = alpha++;
}

public static void getRand(){
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100 = (int) (randomNum * 100);
randNSEW = x100 % 4;
}

public int getNextRow(){
return nextrow;
}

public int getNextCol(){
return nextcol;
}

public boolean processing(){
for(int i = 0; i < 25; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
}
if(randNSEW == 1){
nextrow++;
}
if(randNSEW == 2){
nextcol++;
}
if(randNSEW == 3){
nextrow--;
}

if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
return false;
}

walkgrid[nextrow][nextcol] = alpha++;
}
return true;
}



public char[][] DisplayGrid() {
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
System.out.print(walkgrid[x][y] + " ");
}
System.out.println();
}
return walkgrid;
}
}

public class WalkTester {

public static void main(String[] args) {
Scanner inpr = new Scanner(System.in);
Scanner inpc = new Scanner(System.in);
Scanner inpchoice = new Scanner(System.in);

int r = 0;
int c = 0;
char choice = 'y';

while(choice == 'y' || choice == 'Y') {
System.out.println("Please enter x coordinate between 1 and 10.");
r = inpr.nextInt();
r = r - 1;

System.out.println("Please enter y coordinate between 1 and 10");
c = inpr.nextInt();
c = c - 1;

if(r < 0 || r > 9 || c < 0 || c > 9){
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
DrunkWalker drunkwalker = new DrunkWalker(r, c);
boolean walkerSucceeded = drunkwalker.processing();
drunkwalker.DisplayGrid();
if(walkerSucceeded) {
System.out.println("You made it home");
} else {
System.out.println("You were arrested");
}

System.out.println("Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
}
}

最佳答案

通过“跳到下一个空格,直到到达一个空格”我认为您的意思是它们继续朝同一方向前进。

所以,在你这样做之前:

walkgrid[nextrow][nextcol] = alpha++;

您需要检查:

if (walkgrid[nextrow][nextcol] == '.')

所以沿着这些思路:

    ...
do {
if (randNSEW == 0) nextcol--;
if (randNSEW == 1) nextrow++;
if (randNSEW == 2) nextcol++;
if (randNSEW == 3) nextrow--;

if ((nextrow < 0) || (nextrow >= 10) || (nextcol < 0) || (nextcol >= 10)) {
return false;
}
if (walkgrid[nextrow][nextcol] == '.') continue; /* try next */
walkgrid[nextrow][nextcol] = alpha++;
} while (false);

或者我们可以将最后三行重写为:

    } while (walkgrid[nextrow][nextcol] == '.');
walkgrid[nextrow][nextcol] = alpha++;

关于java - 在执行操作之前检查数组的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com