gpt4 book ai didi

java - toString方法我不知道如何使用它以及目的是什么

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:48 25 4
gpt4 key购买 nike

这是头等舱:

public class Book {



public int n1,n2,g1,g2;
public boolean lamp=false;
public int isbn;
public String author,title,genre;


public void generateReference(){


char n1=author.charAt(0);
char n2=author.charAt(1);
char g1=genre.charAt(0);
char g2=genre.charAt(1);
System.out.print(n1+""+n2+"-"+g1+""+g2);

}

public boolean verifyisbn(int x){
isbn=x;

int d1=isbn%10;
isbn=isbn/10;
int d2=isbn%10;
isbn=isbn/10;
int d3=isbn%10;
isbn=isbn/10;
int d4=isbn%10;

int sum=(d4*3+d3*2+d2*1)%4;
if(sum==d1){

return true;

}
return false;

}


public String toString(){

return n1+""+n2+""+g1+""+g2+""+author+""+isbn+""+title+""+genre;








}

}

=====

这里是我应该创建对象并打印用户输入的信息的地方

import java.util.Scanner;
public class TestBook {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);
Book b1=new Book();
System.out.println("Please, entre the book details #ISBN, author,title, and genre.");
boolean a=b1.verifyisbn(b1.isbn=input.nextInt());
if(a==true){
b1.author=input.next();
b1.title=input.next();
b1.genre=input.next();
System.out.println("Title: "+b1.title);
System.out.println("Author: "+b1.author);
System.out.println("ISBN: "+b1.isbn+" - "+"Reference Code :"+b1.n1+""+b1.n2+""+b1.g1+""+b1.g2);
System.out.println("Genre: "+b1.genre);
b1.generateReference();

} else {
System.out.print("Invalid ISBN ");
}
}

}

但我遇到的问题是数字是 00000

我怎样才能让他们打印?

最佳答案

您必须将其更改为

public class Book {
public char n1,n2,g1,g2;
public int isbn;
public String author,title,genre;

Book(String author,String title, String genre,int isbn){
this.author = author;
this.title = title;
this.genre = genre;
this.isbn = isbn;
}

...

public void generateReference(){
n1=author.charAt(0);
n2=author.charAt(1);
g1=genre.charAt(0);
g2=genre.charAt(1);
System.out.print(n1+""+n2+"-"+g1+""+g2);
}

...

@Override
public String toString(){
return n1+""+n2+""+g1+""+g2+""+author+""+isbn+""+title+""+genre;
}

关于java - toString方法我不知道如何使用它以及目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40831722/

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