首页 > 解决方案 > 类型不匹配:无法从字符串转换为(方法名称)

问题描述

我不断收到一条错误消息,提示类型不匹配:无法从字符串转换为(方法名称)。我创建了一个名为 Creature 的类并创建了一个构造函数,我刚刚创建了一个数组来存储构造函数中的生物的名称,但它只是不起作用。我希望有一个人可以帮助我。太感谢了。

//here is my constructor but it's in another class (class creature).


public Creature(String name) {
        this.name = name;

        /*
         * random function returns a value between 0.0 and 1 adding 1 makes sure that 0
         * is never returned Math.floor manages to return integer value closest and
         * smaller to the current value e.g 3.45 -> on floor function 3 is the answer
         */
        foodUnits = (int) Math.floor(Math.random() * 12 + 1); // get a value from 1 - 12
        healthUnits = (int) Math.floor(Math.random() * 7 + 1); // get a random value from 1 - 7
        firePowerUnits = (int) Math.floor(Math.random() * 11); // get a random value from 0 - 10
        // normalize units function
        dateCreated = new Date();
        dateDied = null;

        // keeps track of how many creatures are alive
        numStillAlive += 1; // adding to an alive creature to the class

    }



import java.util.Scanner;

public class Driver {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int NumOfCreatures;
        String NameOfCreature;

        Creature[] c = new Creature[NumOfCreatures];

        System.out.println("[------------------------------------------------]\n" + "[ Welcome to the Battle Game ]\n"
                + "[------------------------------------------------] ");
        do {
            System.out.println("How many creatures would you like to have (minimum 2, maximum 8)?");

            NumOfCreatures = sc.nextInt();
            if (NumOfCreatures < 2 || NumOfCreatures > 8) {
                System.out.println("*** Illegal number of creatures requested *** ");
            }
        } while (NumOfCreatures < 2 || NumOfCreatures > 8);

        for (int i = 1; i <= NumOfCreatures; i++) {
            System.out.println("What is the name of creature " + i + "?");
            NameOfCreature = sc.nextLine();
            c[i] = NameOfCreature;

        }

    }

}

标签: java

解决方案


此行是您c[i] = NameOfCreature;尝试将 a 保存StringCreature数组的问题。

相反,您需要这样做:

//Create a new Creature using the input
Creature myCreature = new Creature(NameOfCreature);
//Now assign the Creature to the array
c[i] = myCreature;

Creature注意,这一行的数组大小也有问题,Creature[] c = new Creature[NumOfCreatures];因为程序运行时的值NumOfCreatures是未定义的(直到稍后在代码中才设置该值)。所以你需要做的是在你得到这样的生物编号后创建数组大小:

    Scanner sc = new Scanner(System.in);
    int NumOfCreatures;
    String NameOfCreature;
    
    //Don't set the size here, just create the object
    Creature[] c;

    System.out.println("[------------------------------------------------]\n" + "[ Welcome to the Battle Game ]\n"
            + "[------------------------------------------------] ");
    
    //Get the creature number so we can define the array size
    do {
        System.out.println("How many creatures would you like to have (minimum 2, maximum 8)?");

        NumOfCreatures = sc.nextInt();
        if (NumOfCreatures < 2 || NumOfCreatures > 8) {
            System.out.println("*** Illegal number of creatures requested *** ");
        }
    } while (NumOfCreatures < 2 || NumOfCreatures > 8);
    
    //Now that we know the number of creatures we can set the array size
    c = new Creature[NumOfCreatures];

    //Then we can fill the array
    for (int i = 1; i <= NumOfCreatures; i++) {
        System.out.println("What is the name of creature " + i + "?");
        NameOfCreature = sc.nextLine();
        //Create a new Creature using the input
        Creature myCreature = new Creature(NameOfCreature);
        //Now assign the Creature to the array
        c[i] = myCreature;
    }

推荐阅读