- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在问我的问题之前,我想澄清一些事情。首先,我是Java和程序设计的新手。其次,这是我的第二篇文章,因此,如果我做错了什么,请放轻松。最后,我想解释一下为什么我做错了,而不是在对此帖子的任何回复中只是粘贴的解决方案。为了更好地理解该问题,我将编写分配信息,然后编写给定的Driver类,然后编写由Driver类访问的我的类代码。
我的问题:
如何使我的“建筑物”的左下角在2D数组上为[0] [0]? Here's一个for循环的示例,该示例可将2D数组的左下角更改为[0] [0],但我尝试将其实现到我的searchRoom方法中(玩家角色设置为myHidingPlaces索引),我可以t将myHidingPlaces [0] [0]设为我的2D数组的左下角。我相信我需要使用for循环以某种方式编辑toString方法,但是我不知道该怎么做。
以下是任务:
您要设计一个类“LostPuppy.java”,该类代表迷失在多层建筑中的小狗,
每个楼层包含相同数量的房间。在实例化(或创建)此类的对象时,
每个楼层的每个房间都将初始化为空(为此,您实际上将使用空格“”
目的),并会在丢失小狗的地方选择一个随机的房间。为此,字符“P”将
被放置在这个随机的位置。下面列出了有关构造函数的更多详细信息。
此类的一个对象用作游戏,两个玩家轮流搜索小狗,一个房间
直到发现不幸的小犬只为止的时间。该对象的实例化和搜索将被执行
通过提供给您的“驱动程序”程序,您只需专注于开发
类(驱动程序在文件“PuppyPlay.java”中)
字段(当然,所有字段都是私有(private)的):
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 "";
}
}
最佳答案
您无法用Java创建反向数组,但是您不需要它。您需要考虑建筑物并对其进行操作,就像使用矩阵一样。但是,您需要颠倒打印。同样,通过您提供的链接也不会更改数组中的位置,这只是从最后一个索引到第一个索引的数组遍历。
因此,您需要上下颠倒地打印阵列,这非常容易。您需要在toString
方法中更改一行,然后更改
for (int r = 0; r < floors; r++)
for (int r = floors - 1; r >= 0; r--)
for (int i = theFloors - 1; i >= 0; i--) {
for (int j = 0; j <= theRooms - 1; j++) {
...
}
}
for (int i = 0; i < theFloors; i++) {
for (int j = 0; j < theRooms; j++) {
...
}
}
indicesOK
方法,因为数组的最后一个索引是它的
length - 1
,因此,当您访问
myHidingPlaces.length
或
myHidingPlaces[i].length
元素时,将抛出
ArrayIndexOutOfBoundsException
异常。
public boolean indicesOK(int floor, int room) {
return (floor < myHidingPlaces.length && room < myHidingPlaces[0].length);
}
关于Java-复杂程序中的2D数组索引操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421459/
我是 C 语言新手,我编写了这个 C 程序,让用户输入一年中的某一天,作为返回,程序将输出月份以及该月的哪一天。该程序运行良好,但我现在想简化该程序。我知道我需要一个循环,但我不知道如何去做。这是程序
我一直在努力找出我的代码有什么问题。这个想法是创建一个小的画图程序,并有红色、绿色、蓝色和清除按钮。我有我能想到的一切让它工作,但无法弄清楚代码有什么问题。程序打开,然后立即关闭。 import ja
我想安装screen,但是接下来我应该做什么? $ brew search screen imgur-screenshot screen
我有一个在服务器端工作的 UDP 套接字应用程序。为了测试服务器端,我编写了一个简单的 python 客户端程序,它发送消息“hello world how are you”。服务器随后应接收消息,将
我有一个 shell 脚本,它运行一个 Python 程序来预处理一些数据,然后运行一个 R 程序来执行一些长时间运行的任务。我正在学习使用 Docker 并且我一直在运行 FROM r-base:l
在 Linux 中。我有一个 c 程序,它读取一个 2048 字节的文本文件作为输入。我想从 Python 脚本启动 c 程序。我希望 Python 脚本将文本字符串作为参数传递给 c 程序,而不是将
对于一个类,我被要求编写一个 VHDL 程序,该程序接受两个整数输入 A 和 B,并用 A+B 替换 A,用 A-B 替换 B。我编写了以下程序和测试平台。它完成了实现和行为语法检查,但它不会模拟。尽
module Algorithm where import System.Random import Data.Maybe import Data.List type Atom = String ty
我想找到两个以上数字的最小公倍数 求给定N个数的最小公倍数的C++程序 最佳答案 int lcm(int a, int b) { return (a/gcd(a,b))*b; } 对于gcd,请查看
这个程序有错误。谁能解决这个问题? Error is :TempRecord already defines a member called 'this' with the same paramete
当我运行下面的程序时,我在 str1 和 str2 中得到了垃圾值。所以 #include #include #include using namespace std; int main() {
这是我的作业: 一对刚出生的兔子(一公一母)被放在田里。兔子在一个月大时可以交配,因此在第二个月的月底,每对兔子都会生出两对新兔子,然后死去。 注:在第0个月,有0对兔子。第 1 个月,有 1 对兔子
我编写了一个程序,通过对字母使用 switch 命令将十进制字符串转换为十六进制,但是如果我使用 char,该程序无法正常工作!没有 switch 我无法处理 9 以上的数字。我希望你能理解我,因为我
我是 C++ 新手(虽然我有一些 C 语言经验)和 MySQL,我正在尝试制作一个从 MySQL 读取数据库的程序,我一直在关注这个 tutorial但当我尝试“构建”解决方案时出现错误。 (我正在使
仍然是一个初学者,只是尝试使用 swift 中的一些基本函数。 有人能告诉我这段代码有什么问题吗? import UIKit var guessInt: Int var randomNum = arc
我正在用 C++11 编写一个函数,它采用 constant1 + constant2 形式的表达式并将它们折叠起来。 constant1 和 constant2 存储在 std::string 中,
我用 C++ 编写了这段代码,使用运算符重载对 2 个矩阵进行加法和乘法运算。当我执行代码时,它会在第 57 行和第 59 行产生错误,非法结构操作(两行都出现相同的错误)。请解释我的错误。提前致谢:
我是 C++ 的初学者,我想编写一个简单的程序来交换字符串中的两个字符。 例如;我们输入这个字符串:“EXAMPLE”,我们给它交换这两个字符:“E”和“A”,输出应该类似于“AXEMPLA”。 我在
我需要以下代码的帮助: 声明 3 个 double 类型变量,每个代表三角形的三个边中的一个。 提示用户为第一面输入一个值,然后 将用户的输入设置为您创建的代表三角形第一条边的变量。 将最后 2 个步
我是新来的,如果问题不好请见谅 任务:将给定矩阵旋转180度 输入: 1 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 输出: 16 15 14 13 12 11
我是一名优秀的程序员,十分优秀!