Java定义一本书有三个成员变量_java编程解答

展开全部

第一个:62616964757a686964616fe59b9ee7ad9431333365653262public class App1 {

static int fib(int n) {

if (n == 0 || n == 1) {

return 1;

}

return fib(n - 1) + fib(n - 2);

}

public static void main(String[] args) {

for (int i = 20; i <= 30; i++) {

System.out.print(Integer.toString(fib(i)) + " ");

}

}

}

第二个:public class App2 {

public static void main(String[] args) {

for (int i = 0; i <= 20; i++) {

for (int j = 0; j <= 33; j++) {

for (int k = 0; k <= 100; k++) {

if (i * 5 + j * 3 + k / 3 == 100 && i + j + k == 100 && k % 3 == 0)

System.out.println("公鸡:" + i + " 母鸡:" + j + " 小鸡:" + k);

}

}

}

}

}

第三个:import java.io.FileWriter;

import java.io.IOException;

import java.util.*;

class Book {

private String title;

private String author;

private int pageCount;

public Book() {

this("", "", 0);

}

public Book(String title, String author, int pageCount) {

this.title = title;

this.author = author;

this.pageCount = pageCount;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public int getPageCount() {

return pageCount;

}

public void setPageCount(int pageCount) {

this.pageCount = pageCount;

}

public void print() {

System.out.println(title + "\t" + author + "\t" + pageCount);

}

public String getBookInfo() {

return "书名:" + title + ",作者:" + author + ",页数:" + pageCount;

}

}

public class LibraryCollectionTest {

private static Scanner scanner = new Scanner(System.in);

private static List books = new ArrayList();

private static Book input() {

Book book = new Book();

System.out.print("请输入书名:");

book.setTitle(scanner.next());

System.out.print("请输入作者:");

book.setAuthor(scanner.next());

System.out.print("请输入页数:");

book.setPageCount(scanner.nextInt());

return book;

}

public static void main(String[] args) throws IOException {

while (true) {

System.out.print("是否添加图书?(true/false)");

if (!scanner.nextBoolean()) {

break;

}

Book book = input();

books.add(book);

}

FileWriter writer = new FileWriter("books.txt");

for (Book book : books) {

writer.write(book.getBookInfo() + System.lineSeparator());

}

writer.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值