当前位置: 首页 > 知识库问答 >
问题:

Java-错误:类“类名”中的构造函数“构造函数名”不能应用于给定类型;

卞俊哲
2023-03-14

在问我的问题之前,我想把一些事情说清楚。首先,我是Java和编程的新手。第二,这是我的第一个帖子,所以如果我做错了什么,请宽容对待我。最后,我不想要任何具体的解决办法,我的任务在任何回应这篇文章。这些问题要我来解决。我想要的是一个解释,为什么我的测试代码不能编译/运行。为了更好地理解这个问题,我将粘贴赋值信息,然后是给定的驱动程序类,然后是驱动程序类访问的我的类代码。我的编译器错误显示在标题中,但由于它是相当模糊的,这里是一个屏幕截图,我得到的确切错误。

以下是作业:

您将设计一个类“LostPuppy.java”,它表示一只小狗在多层建筑中丢失,该多层建筑包含相同数量的房间。在实例化(或创建)这个类的对象时,每个楼层的每个房间都将被初始化为空(您实际上将为此使用space“”字符),并且会在小狗丢失的地方选择一个随机房间。为此,字符“P”将被放置在这个随机位置。关于构造函数的详细信息如下所示。

这个类的一个对象被用作两个玩家轮流寻找小狗的游戏,一次一个房间,直到找到这只不幸的小狗为止。这个对象的实例化和搜索将由提供给您的“驱动程序”执行,允许您只需集中精力开发类(驱动程序在文件“puppyplay.java”中)

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

>

  • 名为MyHidingPlaces的字符(char)数组。这表示每层的行是楼层,列是房间的建筑(这栋建筑有一个不寻常的编号系统;楼层和房间都是从零开始的)。

    两个整数将保存小狗丢失的楼层和房间,命名为myFloorLocation和MyRoomLocation。

    一个名为myWinner的char,当玩家找到小狗时,它将被分配给玩家的角色(驱动程序使用数字“1”和“2”来更清楚地区分玩家和小狗)。

    一个名为myFound的布尔值,当发现小狗时,该值设置为true。

    构造函数:

    >

  • 接收两个整数参数,作为用户输入的小狗丢失的建筑物的楼层数和房间数。

    构造函数将2D数组“myhidingplaces”实例化为字符数组,其中第一个参数用于行(theflores),第二个参数用于列(theRooms)。

    初始化MyHidingPlaces“单元格,每个单元格包含空格”“(用单引号完成)

    方法:

    >

  • RoomSearchedEviety接收要搜索的楼层和房间,如果房间已经搜索过,则返回true,否则返回false。

    puppyLocation接收要搜索的楼层和房间,如果楼层和房间是小狗丢失的地方,则返回true,否则返回false。此方法不应更改任何字段。

    indicesOK接收要搜索的楼层和房间,如果楼层和房间值在数组索引范围内,则返回true,否则返回false(用于检查这些索引应用于数组时不会导致错误)。

    numberOfFloors返回建筑物有多少层(第一层从零开始)。

    numberOfRooms返回建筑物每层有多少个房间(第一个房间从零开始,所有楼层都有相同数量的房间)。

    searchRoom接收要搜索的楼层和房间,以及当前的玩家(作为char类型),如果发现了小狗,则返回true,否则返回false。如果找不到小狗,searchRoom还将接收到的楼层和房间位置处的myHidingPlaces数组设置为接收到的玩家值(“1”或“2”),或者在找到时,将myWinner字段设置为当前玩家,并将myFound设置为true。

    toString显示当前的hidingPlaces数组及其内容,除了小狗的位置,它一直保持隐藏,直到他/她被找到为止,此时toString将被调用(由驱动程序调用),并且发现小狗的玩家和一个“P”将显示在同一个单元格中…。

    现在,也许还有toString输出的尴尬部分。通常,在显示二维数组时,[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
    {
    
       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";
    
       }
    
    }
    
  • 共有1个答案

    萧丁雨
    2023-03-14

    我不想要任何具体的解决办法,我的任务在任何回应这个职位。这些问题要我来解决。我想要的是一个解释,为什么我的测试代码不能编译/运行。

    这一行

    game = new LostPuppy(totalFloors, totalRooms);
    

    不会编译,因为您没有定义任何将两个int作为参数的构造函数(totalflores和totalRooms)。

    为了解决这个问题,请在类LostPuppy中声明一个构造函数,如

    public LostPuppy(int totalFloors, int totalRooms)
    {
        //Do something here with paremeters..
    }
    

    这一行

    while (!game.indicesOK(floor, room) 
    

    不会编译,因为您尚未在LostPuppy类中定义任何IndiceSok方法。

    创建一个方法,如

    public boolean indicesOK(int floot, int room)
    {
        //return some conditions
    }
    
     类似资料:
    • 我有2个子类:职员、学生,他们属于超类人。 以下是我的老师给出的代码(任务): 我不知道我可以输入什么来创建一个没有参数的对象。它总是出现这样的错误:Person类中的构造函数Person不能应用于给定的类型;必选:java.lang.String,int 我在网上查过,有两种方法可以解决这个问题: > < li> 在超类中添加默认值:< code > Person()//不带参数。 在子类学生中

    • 我得到了下面的代码,使用数组来查找一些prim数。然而,当试图编译我的用户类PalindromeArrayUser时,它说——“类中的构造函数不能应用于给定的类型” 要求:int。找到:没有论点。原因:实际参数和正式参数列表的长度不同。 但是,我已经向构造器传递了一个int值(与我的蓝图中设计的方式相同)。我不太明白问题来自哪里。谢谢。 这是我的两节课 而这就是我的用户类问题的来源。上面的类编译良

    • 问题内容: 我有以下Java代码: 而且我不断收到错误消息:“无法将构造函数应用于给定类型” …这是否意味着超类的子类在构造函数中必须具有与超类相同数量的参数?我已经把头撞在墙上一个小时了。 问题答案: 子类不必有“相同数量的构造为超参数”任何构造函数,但它 确实 需要调用一些它的父类的构造函数从自己的构造。 如果超类具有no- arg构造函数,则默认情况下会被调用,如果省略了对超类构造函数的显式

    • 我正在做作业,所以我只想修复我的编译错误,这样我就可以继续工作了。我需要创建一个PointList类,在ArrayList中保存一个Point对象列表。PointList类应该接受任何作为Point类实例或Point子类的对象。 我不断收到一个编译器错误,上面写着 我真的不明白我错过了什么,我已经通读了这本书,似乎不明白为什么我会得到这个错误。我已经制作了 Point 类并完成了测试,但似乎无法编

    • 我正在制作一个配方应用程序,我遇到了这个错误:HomeItem类中的构造函数HomeItem不能应用于给定的类型;配方添加(新HomeItem(“viay”,R.drawable.vietnamese)); 这是我的代码: 公共类HomeFragment扩展了片段{View v;private RecyclerView myrecyclerview;private List recipe;publ

    • 我一开始就宣布了超级权利。但它仍然显示了错误,即它不是。此外,父类中的构造函数具有与“super()”中的构造函数相同的参数。它仍然说参数不匹配