Tra*_*ton 4 java arrays constructor
在提出我的问题之前,我想先澄清一些事情.首先,我是Java和编程的新手.第二,这是我的第一篇文章,所以如果我做错了什么,请放轻松.最后,在本文的任何回复中,我都不希望任何具体的解决方案.那些问题让我想出来.我想要的是解释为什么我的测试代码不会编译/运行.为了更好地理解这个问题,我将粘贴赋值信息,然后粘贴给定的Driver类,然后粘贴Driver类访问的类代码.我有的编译器错误显示在标题中,但由于它相当模糊,这里是我得到的确切错误的屏幕截图.
以下是作业:
您将设计一个类"LostPuppy.java",它代表在多层建筑物中丢失的小狗,每层楼的房间数量相同.在实例化(或创建)该类对象的过程中,每层楼的每个房间都将被初始化为空(为此目的,您将实际使用空间''字符),并且将选择一个随机房间,其中小狗丢失.为此,字符"P"将被放置在该随机位置.有关构造函数的更多详细信息如下所示.
这个类的一个对象被用作两个玩家的游戏,轮流搜索小狗,一次一个房间,直到发现不幸的小犬.该对象的实例化和搜索将由一个"驱动程序"程序执行,该程序已经提供给您,允许您只需要集中精力开发该类(驱动程序在文件"PuppyPlay.java"中)
字段(当然,所有字段都是私有的):
一个名为myHidingPlaces的字符(char)数组.这代表建筑物,其中行是地板,列是每层楼的房间(这个建筑物有一个不寻常的编号系统;地板和房间都从零开始).
两个整数将占据小狗丢失的地板和房间,名为myFloorLocation和myRoomLocation.
一个名为myWinner的char,当玩家找到小狗时,它将被分配玩家的角色(驾驶员程序使用数字'1'和'2'来更清楚地区分玩家和小狗).
名为myFound的布尔值,在找到小狗时设置为true.
构造函数:
接收两个整数参数作为用户的输入,用于丢失小狗的建筑物的楼层数和房间数.
构造函数将2D数组"myHidingPlaces"实例化为字符数组,其中第一个参数用于行(theFloors),第二个参数用作列(theRooms).
初始化myHidingPlaces的单元格,每个单元格包含一个空格''(用单引号完成)
方法:
roomSearched已经收到要搜索的楼层和房间,如果房间已被搜索则返回true,否则返回false.
puppyLocation接收要搜索的楼层和房间,如果楼层和房间是小狗丢失的地方,则返回true,否则返回false.此方法不应更改任何字段.
indicesOK接收要搜索的楼层和房间,如果楼层和房间值在数组索引范围内,则返回true,否则返回false(用于检查这些索引在应用于数组时不会导致错误).
numberOfFloors返回建筑物中的楼层数(第一层从零开始).
numberOfRooms返回建筑物每层楼的房间数量(第一个房间从零开始,所有楼层都有相同数量的房间).
searchRoom接收要搜索的楼层和房间以及当前玩家(作为char类型),如果找到小狗则返回true,否则返回false.如果未找到小狗,searchRoom还会将收到的楼层和房间位置的myHidingPlaces数组设置为收到的玩家值('1'或'2')或者,当找到时,将myWinner字段设置为当前玩家并设置myFound为true.
toString显示当前的hidePlaces数组及其内容除了小狗的位置之外,小狗的位置一直隐藏,直到找到他/她将被调用的字符串(由驱动程序调用)并且发现小狗的玩家和'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"));
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这是我的测试代码:
import java.util.Random;
import java.util.Scanner;
public class LostPuppy
{
char[][] myHidingPlaces;
int myFloorLocation;
int myRoomLocation;
char myWinner;
boolean myFound;
Random random = new Random();
public void LostPuppy(int theFloors, int theRooms)
{// this ^ void is the issue and is now removed from my code(answered 7/14/2015 on stack overflow)
char[][] myHidingPlaces = new char[theFloors][theRooms];
for (int i = 0; i < theFloors; i++)
{
for (int j = 0; j < theRooms; 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)
{
if (myHidingPlaces[floor][room] == '1' ||
myHidingPlaces[floor][room] == '2')
{
return true;
}
else
{
return false;
}
}
public boolean puppyLocation(int floor, int room)
{
if (myHidingPlaces[floor][room] == 'P')
{
return true;
}
else
{
return false;
}
}
public boolean indicesOK(int floor, int room)
{
if (floor <= myHidingPlaces.length || room <= myHidingPlaces[0].length)
{
return true;
}
else
{
return false;
}
}
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 (myFound = true)
{
myWinner = player;
myFound = true;
return true;
}
else
{
myHidingPlaces[floor][room] = player;
return false;
}
}
public String toString()
{
return "this is a test";
}
}
Run Code Online (Sandbox Code Playgroud)
在本文的任何回复中,我都不希望任何具体的解决方案.那些问题让我想出来.我想要的是解释为什么我的测试代码不会编译/运行.
这条线
game = new LostPuppy(totalFloors, totalRooms);
Run Code Online (Sandbox Code Playgroud)
将无法编译,因为您尚未定义任何期望两个int作为参数的构造函数(totalFloors和totalRooms).
为了解决这个问题,在你的类中声明LostPuppy一个构造函数,如
public LostPuppy(int totalFloors, int totalRooms)
{
//Do something here with paremeters..
}
Run Code Online (Sandbox Code Playgroud)
这条线
while (!game.indicesOK(floor, room)
Run Code Online (Sandbox Code Playgroud)
无法编译,因为您没有indicesOk在LostPuppy类中定义任何方法.
创建一个方法,如
public boolean indicesOK(int floot, int room)
{
//return some conditions
}
Run Code Online (Sandbox Code Playgroud)