java布尔矩阵程序_Java – 复杂程序中的二维数组索引操作

本文介绍了一个Java编程问题,涉及在二维数组中实现布尔矩阵,并处理2D数组的索引操作。作者是一名Java新手,正在解决如何在LostPuppy类中设置2D数组的左下角为[0][0]。文章详细阐述了类的构造函数、方法以及提供的Driver类,展示了如何在游戏中搜索小狗的过程。同时,文章也包含了LostPuppy类的实现代码,包括初始化2D数组、搜索房间的方法等。
摘要由CSDN通过智能技术生成

在提出我的问题之前,我想先澄清一些事情.首先,我是

Java和编程的新手.第二,这是我的第二篇文章,如果我做错了什么,请放轻松.最后,我想解释为什么我所做的是错误的,而不仅仅是在对这篇文章的任何回复中的粘贴解决方案.为了更好地理解这个问题,我将编写赋值信息,然后是给出的Driver类,然后是Driver类访问的类代码.

我的问题:

如何让我的’建筑’的左下角在我的2D阵列上为[0] [0]? Here’s一个for循环的例子,它可以将2D数组的左下角改为[0] [0],但是我尝试将它实现到我的searchRoom方法中(其中玩家角色设置为myHidingPlaces索引)我可以’ t my myPidingPlaces [0] [0]是我2D阵列的左下角.我相信我需要以某种方式使用for循环编辑toString方法,但我无法弄清楚我应该怎么做.

以下是作业:

你要设计一个类“LostPuppy.java”,它代表了一个在多层建筑中丢失的小狗

每层楼的房间数量相同.在实例化(或创建)此类对象的过程中,

每层楼的每个房间都会被初始化为空(你实际上会使用空格”字符

目的)并且将选择一个随机的房间,在那里小狗失踪.为此,字符“P”将

被放置在这个随机位置.有关构造函数的更多详细信息如下所示.

这个类的一个对象被用作两个玩家轮流搜索小狗的游戏,一个房间在一个

时间,直到发现不幸的小犬.将执行此对象的实例化和搜索

通过提供给您的“驱动程序”程序,您只需专注于开发

类(驱动程序在文件“PuppyPlay.java”中)

字段(当然,所有字段都是私有的):

>名为myHidingPlaces的字符(char)数组.这表示行是地板的建筑物

每个楼层的柱子都是房间(这栋楼有一个不寻常的编号系统;楼层和楼层

房间都从零开始.

>两个整数将占据小狗丢失的地板和房间,名为myFloorLocation和

myRoomLocation.

>一个名为myWinner的char,当玩家找到小狗时,它将被分配玩家的角色

(驾驶员程序使用数字’1’和’2’来更清楚地区分玩家和小狗).

>名为myFound的布尔值,在找到小狗时设置为true.

构造函数:

>接收两个整数参数作为用户对建筑物楼层数和房间数的输入

小狗迷路了.

>构造函数将2D数组“myHidingPlaces”实例化为具有第一个参数的字符数组

对于行(theFloors)和第二个参数作为列(theRooms).

>初始化myHidingPlaces的单元格,每个单元格包含一个空格”(用单引号完成)

>使用第一个参数随机设置myFloorLocation(地板小狗开启)

>使用第二个参数随机设置myRoomLocation(房间小狗在)

>将myHidingPlaces [myFloorLocation] [myRoomLocation]设置为char’P’

>将myWinner设置为单个空格

>将myFound设置为false

方法:

> roomSearched已经收到要搜索的楼层和房间,如果房间有,则返回true

已被搜查,否则为假.

> puppyLocation接收楼层和待搜索的房间,如果楼层和房间都是,则返回true

小狗丢失的地方,否则就是假的.此方法不应更改任何字段.

> indicesOK接收要搜索的楼层和房间,如果楼层和房间值是,则返回true

在数组索引范围内,否则为false(用于检查这些索引不会导致错误)

当应用于数组时).

> numberOfFloors返回建筑物中的楼层数(第一层从零开始).

> numberOfRooms返回建筑物每层楼的房间数量(第一个房间开始于

零和所有楼层都有相同数量的房间).

> searchRoom接收要搜索的楼层和房间以及当前播放器(作为字符类型)

如果找到小狗则返回true,否则返回false.如果没有找到小狗searchRoom也

将收到的楼层和房间位置的myHidingPlaces数组设置为收到的播放器值(‘1’

或者’2′)OR,当找到时,将myWinner字段设置为当前播放器并将myFound设置为true.

> toString显示当前的hidePlaces数组及其内容除了小狗的位置

在他/她被发现之前,它将保持隐藏状态.string将被调用(由驱动程序)和两者

找到小狗的玩家和’P’将显示在同一个单元格中….

>现在,也许是toString输出的尴尬部分.通常,在显示2D数组时,

[0] [0]单元格与矩阵一样显示在左上角.但是,因为小狗

决定迷失在建筑物而不是矩阵中,一楼的视觉感觉会更加明显

(第0行)显示在它上面的底部,第二层……最后是顶层,好……顶部!至

保存单词,仔细查看下一页提供的示例运行.你的输出应该看起来像

与示例运行的下一页中显示的内容相同.

这是驱动程序:

import java.util.Random;

import java.util.Scanner;

/**

* This program is used as a driver program to play the game from the

* class LostPuppy. Not to be used for grading!

*

* A puppy is lost in a multi-floor building represented in the class

* LostPuppy.class. Two players will take turns searching the building

* by selecting a floor and a room where the puppy might be.

*

* @author David Schuessler

* @version Spring 2015

*/

public class PuppyPlay

{

/**

* Driver program to play LostPuppy.

*

* @param theArgs may contain file names in an array of type String

*/

public static void main(String[] theArgs)

{

Scanner s = new Scanner(System.in);

LostPuppy game;

int totalFloors;

int totalRooms;

int floor;

int room;

char[] players = {'1', '2'};

int playerIndex;

boolean found = false;

Random rand = new Random();

do

{

System.out.print("To find the puppy, we need to know:\n"

+ "\tHow many floors are in the building\n"

+ "\tHow many rooms are on the floors\n\n"

+ " Please enter the number of floors: ");

totalFloors = s.nextInt();

System.out.print("Please enter the number of rooms on the floors: ");

totalRooms = s.nextInt();

s.nextLine(); // Consume previous newline character

// Start the game: Create a LostPuppy object:

game = new LostPuppy(totalFloors, totalRooms);

// Pick starting player

playerIndex = rand.nextInt(2);

System.out.println("\nFloor and room numbers start at zero '0'");

do

{

do

{

System.out.println("\nPlayer " + players[playerIndex]

+ ", enter floor and room to search separated by a space: ");

floor = s.nextInt();

room = s.nextInt();

//for testing, use random generation of floor and room

//floor = rand.nextInt(totalFloors);

//room = rand.nextInt(totalRooms);

} while (!game.indicesOK(floor, room)

|| game.roomSearchedAlready(floor, room));

found = game.searchRoom(floor, room, players[playerIndex]);

playerIndex = (playerIndex + 1) % 2;

System.out.println("\n[" + floor + "], [" + room + "]");

System.out.println(game.toString());

s.nextLine();

} while (!found);

playerIndex = (playerIndex + 1) % 2;

System.out.println("Great job player " + players[playerIndex] +"!");

System.out.println("Would you like to find another puppy [Y/N]? ");

}

while (s.nextLine().equalsIgnoreCase("Y"));

}

}

最后,这是我的测试代码:

import java.util.Random;

import java.util.Scanner;

public class LostPuppy

{

int value;

char[][] myHidingPlaces;

int myFloorLocation;

int myRoomLocation;

char myWinner;

boolean myFound;

Random random = new Random();

public LostPuppy(int theFloors, int theRooms)

{

myHidingPlaces = new char[theFloors][theRooms];

for (int i = theFloors - 1; i >= 0; i--)

{

for (int j = 0; j <= theRooms - 1; j++)

{

myHidingPlaces[i][j] = ' ';

}

}

myFloorLocation = random.nextInt(theFloors);

myRoomLocation = random.nextInt(theRooms);

myHidingPlaces[myFloorLocation][myRoomLocation] = 'P';

myWinner = ' ';

myFound = false;

}

public boolean roomSearchedAlready(int floor, int room)

{

return (myHidingPlaces[floor][room] == '1' ||

myHidingPlaces[floor][room] == '2');

}

public boolean puppyLocation(int floor, int room)

{

return (myHidingPlaces[floor][room] == 'P');

}

public boolean indicesOK(int floor, int room)

{

return (floor <= myHidingPlaces.length && room <= myHidingPlaces[0].length);

}

public int numberOfFloors()

{

return myHidingPlaces.length - 1;

}

public int numberOfRooms()

{

return myHidingPlaces[0].length - 1;

}

public boolean searchRoom(int floor, int room, char player)

{

if (puppyLocation(floor, room))

{

myFound = true;

myWinner = player;

return true;

}

else

{

myHidingPlaces[floor][room] = player;

return false;

}

}

public String toString()

{

int rooms = myHidingPlaces[0].length;

int floors = myHidingPlaces.length;

System.out.print(" ");

for (int x = 0; x < rooms; x++)

{

System.out.print("___");

}

for (int y = 0; y < rooms - 1; y++)

{

System.out.print("_");

}

System.out.print("\n");

for (int r = 0; r < floors; r++)

{

System.out.print("| ");

for (int c = 0; c < rooms; c++)

{

if (myHidingPlaces[r][c] == 'P' && myFound)

{

System.out.print("" + myWinner + "" + myHidingPlaces[r][c] + "| ");

}

else if (myHidingPlaces[r][c] == 'P' && !myFound)

{

System.out.print(" | ");

}

else

{

System.out.print(myHidingPlaces[r][c] + " | ");

}

//System.out.print(myHidingPlaces[r][c] + " | ");

}

System.out.print("\n");

for (int i = 0; i < rooms; i++)

{

System.out.print("|___");

}

System.out.print("|\n");

}

return "";

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值