此java代码中的逻辑错误,如何解决此错误?
} } } }此java代码中的逻辑错误,如何解决此错误?,java,Java,} } } }在此处输入代码 public class DogOwner extends Dog { public static void main(String[] args){ Dog dog1 = new Dog (); Dog dog2 = new Dog(); Dog dog3 = new Dog(); dog1.setLegs(4); dog1.setTail(true); dog3.setFur(" furry fur");
在此处输入代码
public class DogOwner extends Dog {
public static void main(String[] args){
Dog dog1 = new Dog ();
Dog dog2 = new Dog();
Dog dog3 = new Dog();
dog1.setLegs(4);
dog1.setTail(true);
dog3.setFur(" furry fur");
System.out.println("this dog has"+ dog1.GetLeg()+"legs");
System.out.println("does this dog have a tail?"+ dog2.Gettail());
System.out.println("this dog has"+ dog3.GetFur());
我想要的结果是:
这只狗有四条腿
这条狗有尾巴吗?是的
这条狗有毛皮
文本true不应该是文本,它应该执行布尔值,在这种情况下,它应该执行true,而不使用量化标记(希望这有意义)。
我是Java的初学者,我似乎无法完全理解如何使用书籍用Java(或任何编程语言)编写代码。我需要别人的帮助才能完全理解编码。另外,请解释这段代码是如何解决的,以便我能够理解这个问题,以及将来如何解决这些类型的问题(希望如此)
提前感谢。您不能将对象放在输出字符串中,然后期望它知道您的意思 当您将常规对象添加到字符串时,它将调用其
toString()
方法,您尚未定义该方法,默认为一组grable
相反,您需要执行类似于
System.out.println(“这只狗有“+dog1.getLegs()+”legs”)代码>,其他的也一样。您似乎没有调用方法,只是将对象插入字符串中-这在大多数情况下都不会以合理的方式工作。尝试调用已实现的“get”方法
enter code here
public class Dog {
/** variables**/
int Legs;
boolean tail;
String Fur;
/** returns the Legs variable (getter) **/
public int GetLeg(){
return Legs;
}
/** stores method variable Llgs within variable legs (setter) **/
public void setLegs(int legs){
this.Legs=legs;
}
/** returns the tail variable (getter) **/
public boolean Gettail(){
return tail;
}
/** stores method variable tail within variable tail (setter) **/
public void setTail(boolean tail){
this.tail=tail;
}
/**because the void does not work with String data type, the type String
* replaces void to make this code work (Hovercraft Full Of Eels, 2013)
* Hovercraft Full Of Eels.2013.why is this code not working in Java?.
* accessed from:http://stackoverflow.com/questions/20588830/why-is-this-code-not-working-in-java.
* stockoverflow.com. [accessed: 14/12/2013]**/
public String GetFur(){
return Fur;
}
/**stores for the method variable within the parameter variable Fur **/
public void setFur(String fur){
this.Fur=fur;
}
}
在打印信息的行上打印对象,而不是对象的某个属性。将第一个System.out.println更改为:
"this dog has " + dog1.GetLegs() + " legs";
"does this dog have a tail? " + dog2.GetTail();
"this dog has " + dog3.GetFur() + " fur";
我想你会知道如何更改其他行:)它几乎可以工作,但唯一的问题是“这只狗有4条腿”似乎无法执行。代码是'System.out.println(“这只狗有“+dog1.GetLeg()+”腿”);“”;“,但如何向显示的文本中添加空格?空格位于字符串文本中:“Dogs”vs“Dogs”,第二个将有一个尾随空格。因此您可以编写“this”+“+”+“dog”+“+”有“+”+dog1.GetLegs()+“+”legs”
System.out.println("this dog has"+ dog1.GetLeg() +"legs");