gpt4 book ai didi

Java-复杂程序中的2D数组索引操作

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:33 24 4
gpt4 key购买 nike

在问我的问题之前,我想澄清一些事情。首先,我是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)的):

  • 名为myHidingPlaces的字符(字符)数组。这代表行是楼层的建筑物
    列是每个楼层的房间(此建筑物采用不同寻常的编号系统;楼层和
    房间都从零开始)。
  • 这两个整数将保存丢失小狗的地板和房间,名为myFloorLocation和
    myRoomLocation。
  • 一个名为myWinner的字符,当玩家找到小狗时,将为其分配角色
    (驱动程序使用数字“1”和“2”来更清楚地区分玩家和幼犬)。
  • 一个名为myFound的 boolean 值,当找到小狗时将其设置为true。

  • 构造函数:
  • 接收两个整数参数作为用户输入的建筑物楼层和房间数
    在其中,小狗迷路了。
  • 构造函数使用第一个参数将2D数组“myHidingPlaces”实例化为字符数组。
    行(theFloors)和第二个参数作为列(theRooms)。
  • 初始化myHidingPlaces单元格,每个单元格包含一个空格‘(用单引号引起)
  • 使用第一个参数
  • 随机设置myFloorLocation(地板小狗处于打开状态)
  • 使用第二个参数
  • 随机设置myRoomLocation(房间里的小狗在里面)
  • 将myHidingPlaces [myFloorLocation] [myRoomLocation]设置为字符“P”
  • 将myWinner设置为单个空格
  • 将myFound设置为false

  • 方法:
  • roomSearchedAlready接收要搜索的楼层和房间,如果房间有
    已被搜索,否则返回false。
  • puppyLocation接收要搜索的楼层和房间,如果楼层和房间为
    小狗丢了的地方,否则为假。此方法不应更改任何字段。
  • indexOK接收要搜索的楼层和房间,如果楼层和房间的值是
    在数组索引范围内,否则为false(用于检查这些索引不会导致错误)
    当应用于数组时)。
  • numberOfFloors返回建筑物中的楼层数(第一层从零开始)。
  • numberOfRooms返回建筑物每个楼层的房间数(第一个房间开始于
    零,并且所有楼层的房间数均相同)。
  • searchRoom接收要搜索的楼层和房间以及当前播放器(以char类型表示)
    如果找到了小狗,则返回true,否则返回false。如果未找到小狗,还可以搜索
    将接收到的楼层和房间位置的myHidingPlaces数组设置为接收到的玩家值(“1”
    或“2”)OR(如果找到),将myWinner字段设置为当前播放器,并将myFound设置为true。
  • toString显示当前的hiddenPlaces数组及其内容(除了小狗的位置)
    这将一直隐藏,直到找到他/她,此时将调用(驱动程序)toString,并且两者都被调用
    找到小狗和“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 "";

    }

    }

    最佳答案

    您无法用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++) {
    ...
    }
    }

    正如@Dante所说,您还需要更正 indicesOK方法,因为数组的最后一个索引是它的 length - 1,因此,当您访问 myHidingPlaces.lengthmyHidingPlaces[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/

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