gpt4 book ai didi

java - 写作课我错过了什么?

转载 作者:行者123 更新时间:2023-12-01 21:49:40 26 4
gpt4 key购买 nike

到目前为止,我已经完成了一个项目,这是我想要的输出:

测试 1

enter image description here

测试 2

enter image description here

以下是说明。“编写一个名为 Book 的类,其中包含标题、作者和页面的实例数据。定义 Book 构造函数以接受并初始化此数据。包括所有实例数据的 setter 和 getter 方法。包括toString 方法返回如下字符串:

Author: name of the author in one word

Title: title in one word

Pages: number of pages

示例:

Author: Lewis

Title: Java

Pages: 806

编写一个 BookTester 类,从键盘获取作者、标题、页面,创建一个书籍对象并将以上内容打印到屏幕上。我如何得到我的答案来显示我的图片测试 1 和测试 2 中显示的预期输出?另外,我错过了什么?

预订

    public class Book {
String author;
String title;
int pages;

public Book(String a, String t, int p) {
author = a;
title = t;
pages = p;
}

public String toString() {
String output;
output = ("Author: " + author + "\n");
output = ("Title: " + title + "\n");
output = ("Pages: " + pages + "\n");
return output;
}
}

图书测试仪

    import java.util.*;
public class BookTester {
public static void main(String[] args) {
String author;
String title;
int pages;
Scanner scan = new Scanner(System.in);
author = scan.nextLine();
title = scan.nextLine();
pages = scan.nextInt();
Book book1 = new Book(author, title, pages);
System.out.println(book1);
}
}

最佳答案

  @Override
public String toString() {
String output ="";
output += "Author: " + author + "\n";
output += "Title: " + title + "\n";
output += "Pages: " + pages + "\n";
return output;
}

您应该连接 (+) 每个值。否则将返回最后一个值(意味着Pages: ---)。

关于java - 写作课我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com