【问题标题】:Store records in an array将记录存储在数组中
【发布时间】:2022-11-21 21:00:00
【问题描述】:

您好我正在尝试将记录存储在数组中,而不是打印存储的内容,但是当我这样做时,程序会打印一些我不理解的内容。

// this is the definition of the class dinosaur with its attributes
class dinosaur
{
    String name;
    int hunger;
    int anger;
}

// this methods is used to create a dinosaur and set the attributes 
public static dinosaur set_dinosaur(dinosaur record, String name, int hunger, int anger)
{
    record.name=name;
    record.hunger=hunger;
    record.anger=anger;
    
    return record;
}

public static void output()
{
    dinosaur [] array_of_records= new dinosaur[3];
    dinosaur dinosauro=new dinosaur();
    String name;
    Scanner name_input=new Scanner(System.in);
    
    System.out.println("Please enter the name of the dinosaur: ");
    name=name_input.nextLine();
    dinosauro.name=name;
    array_of_records[0]=dinosauro;

    System.out.println("this is the name: " + array_of_records[0]);
    
    
    return;

}

output();

这是打印的内容:

Please enter the name of the dinosaur: 
alex
this is the name: REPL.$JShell$12$dinosaur@2db3c8d3 ```

【问题讨论】:

    标签: java arrays record


    【解决方案1】:

    您需要创建一个 toString() 方法。当您准确地打印恐龙时,如果您创建 toString() 方法,它将被调用。

    这是一些例子: https://www.javatpoint.com/understanding-toString()-method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      相关资源
      最近更新 更多