Java 如何让我的程序打印出来?
在AP计算机科学课程中,我很难让我的程序正常运行。代码中已经有注释来显示我的问题。谢谢各位 Java类 这是我的驾驶课:Java 如何让我的程序打印出来?,java,string,object,tostring,Java,String,Object,Tostring,在AP计算机科学课程中,我很难让我的程序正常运行。代码中已经有注释来显示我的问题。谢谢各位 Java类 这是我的驾驶课: 我很困惑,为什么在运行程序时什么也不显示。请求字符串输入后什么也不显示,因为您只是以Dino.toString()的身份在DinoMain中进行调用。这只是为对象创建字符串表示。它不会打印出该字符串表示形式。如果要将其更改为System.out.println(Dino.toString())您将看到结果 请求字符串输入后不会显示任何内容,因为您只是在DinoMain中以Di
我很困惑,为什么在运行程序时什么也不显示。请求字符串输入后什么也不显示,因为您只是以
Dino.toString()的身份在DinoMain中进行调用。这只是为对象创建字符串表示。它不会打印出该字符串表示形式。如果要将其更改为System.out.println(Dino.toString())代码>您将看到结果 请求字符串输入后不会显示任何内容,因为您只是在DinoMain中以Dino.toString()
的形式进行调用。这只是为对象创建字符串表示。它不会打印出该字符串表示形式。如果要将其更改为System.out.println(Dino.toString())代码>您将看到结果 打印报表在哪里。您正在创建一个对象,然后在该对象上调用toString()
。是否声明一个与其类同名的字段?这是一个新低…打印语句在哪里。您正在创建一个对象,然后在该对象上调用toString()
。是否声明一个与其类同名的字段?这是一个新的低点…实际上,它应该仍然打印出一些东西,因为toString()
方法调用getHunger()
,它打印出一些东西。是的,它打印出getHunger()方法,但它只是在我的问题之后停止了。@Sweetcharge好的,这是有道理的。当你说什么都没有出现时,我很担心。实际上,它应该仍然打印出一些东西,因为toString()
方法调用了getHunger()
,它打印出了getHunger()方法,但在那之后它就停止了,这是我的问题。@Sweetcharge好的,这是有道理的。当你说什么也没出现时,我很担心。
import java.util.*;
public class Allosaur extends Dinosaur
{
private boolean hungry;
private String response, answer;
private String Allosaur;
// Prompt asks for 3 constructors: A Default constructor, a constructor with just a name, and a constructor with a name and hunger "response"
public Allosaur()
{
}
public Allosaur(String name)
{
Allosaur=name;
}
public Allosaur(String name, boolean hungry)
{
Allosaur=name;
this.hungry=hungry;
}
// Used this method to "find out" whether the dinosaur is hungry or not
public boolean getHunger()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Are you hungry? ");
response = keyboard.next();
if(response.equals("Yes"))
hungry = true;
else if(!response.equals("Yes"))
hungry= false;
return hungry;
}
// Asks us to print out "RRRRRRR" if the dinosaur is NOT hungry and "HUNGRRRRRRRY" if the dinosaur IS hungry
public String roar()
{
if(hungry == true)
answer = "HUNGRRRRRRRY";
else if(hungry == false)
answer = "RRRRRRR";
return answer;
}
//When I use the toString() method in my driver class, none of these pop up, why?
public String toString()
{
String Dino = super.toString();
Dino = "The Dinosaur's name is: " + Allosaur;
Dino += "Is the Dinosaur hungry? :" + getHunger() + "\n" + roar();
return Dino;
}
}
public class DinosaurMain extends Allosaur
{
public static void main(String[] args)
{
Allosaur Dino = new Allosaur("Jacob");
Dino.toString();
}
}