【发布时间】:2014-04-11 02:43:00
【问题描述】:
public class Pig {
private int pigss;
private Pig[] pigs;
public Pig[] pigNumber (int pigss)
{
pigs = new Pig [pigss];
return pigs;
}
包含 main 方法的代码:
public class animals{
public static void main(String[] args){
Pig cool = new Pig();
Scanner keyboard = new Scanner (System.in);
System.out.println("How many pigs are there?");
int pigss = Integer.parseInt( keyboard.nextLine() );
cool.pigNumber(pigss);
//This is where I have trouble. I want to use the array pigs here in the main method, this is what i tried:
Pig[] pigs = cool.pigNumber(pigss);
然后我尝试使用 for 循环并将值 (String) 分配给数组的索引 (pigs[])。但给我的错误是:无法从字符串转换为猪。任何提示表示赞赏。谢谢。
for(int j = 0; j < pigs.length; j++)
{
System.out.println("What is the pig " + (j+1) + "'s name");
pigs[j] = keyboard.nextLine();
}
【问题讨论】:
-
错误是为了显示。请发到这里谢谢
-
请发布您的
Pig构造函数。 -
类型不匹配:无法从 String 转换为 Pig。
-
哪一行给出“类型不匹配:无法从字符串转换为猪”?
-
@user3503794 不,我们的意思是在您的问题中发布错误的完整堆栈跟踪。转到底部的编辑并在那里发布。还包括埃利奥特所说的