【发布时间】:2019-03-01 23:33:22
【问题描述】:
嗯,我有很多人。 它们包含实例变量,例如:NAME、AGE、LOCATION。 这些是私有实例变量。
现在我的数组中有很多人都有自己的实例变量。 我会用自己来让事情变得更容易。
奥斯卡 22 美国
但是,在成功创建我的数组后,我需要创建一个布尔方法,该方法允许我搜索整个数组,找到我的名字并将美国更改为日本(或其他)。所以如果奥斯卡在日本,没问题,但如果奥斯卡在美国,就把它改成日本。
这个项目据说是一个相当简单的项目,它不是真正的家庭作业,因为它是一个挑战,但距离教授分配它已经一周了,我无法理解它。我知道如何使用其他方法,但布尔值让我很头疼。
public class Frogs {
private String breed;
private String nativeTo;
public Frogs(String breed, String nativeTo)
{
this.breed=breed;
this.nativeTo=nativeTo;
}
//accessor(GETTER)
public String getRegion()
{
return nativeTo;
}
public String getBreedName()
{
return breed;
}
public String toString() ////(RETURNS A STRING (not the memory) ////////////
{
return ("Frog Breed - " +breed+ "From: " +nativeTo);
}}
public class WildAnimalsDemo {
public static void main(String[]args)
{
Frogs [] stuff = new Frogs[6]; //array of objects of type frogs
stuff[0] = new Frogs ("Tree Frog " , "NJ");
stuff[1] = new Frogs ("Lizard Frog " , "PA");
stuff[2] = new Frogs ("Kermit Frog " , "Muppets");
stuff[3] = new Frogs ("Bear Frog " , "Philipines");
stuff[4] = new Frogs ("Bull Frog " , "New York");
stuff[5] = new Frogs ("Lizard " , "PA"); // DOES NOT HAVE "FROG" IN IT
System.out.println(stuff[0]);
System.out.println(stuff[1]);
System.out.println(stuff[2]);
System.out.println(stuff[3]);
System.out.println(stuff[4]);
System.out.println(stuff[5]);
System.out.println();
//////////////////////////////////////////////////////////////////////////////////////////////////1stPRINT
int count=findFrogFromPlace(stuff,"New York");
System.out.println("Found: " + count);
//////////////////////////////////////////////////////////////////////
int count1=findFrogFromPlace(stuff,"PA");
System.out.println("Found: " + count1);
/////////////////////////////////////////////////////////////////////////////////////////////////2ndPrint
/* boolean possible = findAndSetBreed(stuff, "Frog");
System.out.println(possible + " Species " + stuff[5] );
boolean possible1 = findAndSetBreed(stuff, "Frog");
System.out.println(possible1 + " Species " + stuff[4] );*/
}
public static int findFrogFromPlace(Frogs[] F , String theRegion)
{
int count=0; //////THIS COUNT IS NOT THE SAME AS COUNT FROM THE MAIN
for(int x=0; x<F.length; x++)
{
//Returns String/
if (F[x].getRegion().equalsIgnoreCase(theRegion) )
count++;
}
return count;
}
/*public static boolean findAndSetBreed(Frogs[] frog, String theBreed)
{
for(int x=0; x<frog.length; x++)
{
if (frog[x].getBreedName().equalsIgnoreCase(theBreed) )
System.out.println(x);
}
return frogg;
} */
}
【问题讨论】:
-
您使用什么语言?到目前为止,您尝试过什么?
-
我正在使用 Java,我正在 Eclipse 上编程。
-
函数什么时候应该返回TRUE?当位置改变了?还是只报告完成状态?
-
如果你能提供一些你迄今为止尝试过的例子,那么人们可能会提供帮助
-
好吧 Oscar 不再来自美国,所以如果它说美国应该是假的,然后应该将其更改为当前位置
标签: java arrays private instance-variables