java - 类型不匹配:无法从字符串转换为(方法名称)
问题描述
我不断收到一条错误消息,提示类型不匹配:无法从字符串转换为(方法名称)。我创建了一个名为 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;
}
}
}
解决方案
此行是您c[i] = NameOfCreature;
尝试将 a 保存String
到Creature
数组的问题。
相反,您需要这样做:
//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;
}
推荐阅读
- vue.js - 如何使用 vue test utils 为 vue-treeselect 编写单元测试
- twilio - Twilio Autopilot 如何捕捉所有通配符
- python - 如何计算所有具有序列号的单词?Python3
- python - For循环在等待/异步一段时间后停止
- extjs - Extjs 6.2.1 的 Socket.io 断开连接问题
- c - 为什么一个 char 数组的值是分配值的两倍?
- javascript - 在同一页面中使用多个 ion-toggle,当切换一个 ion-toggle(A) 时,有时会触发另一个 ion-toggle(B) 的 onIonChange 功能
- google-schemas - 架构中不需要的音译 - 如何避免?
- python - 使用 bs4 进行网页抓取
- microsoft-graph-api - MS Graph API 将事件元数据传播到邀请