【问题标题】:toString method I don't know how to use it and what the purposetoString方法我不知道怎么用,目的是什么
【发布时间】:2017-04-11 10:23:10
【问题描述】:

这是第一堂课:

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

我怎么能让他们打印?

【问题讨论】:

  • 有什么建议吗?可以帮我打印 0000 的字符和数字吗?
  • 数字为 0,因为这是它们的默认值,您永远不会将它们设置为其他任何值。

标签: java tostring


【解决方案1】:

你必须把它改成

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;
    }

【讨论】:

  • 好的,我尝试像这样编辑代码 public String toString(){ System.out.println("Title: "+title); System.out.println("作者:"+作者); System.out.println("ISBN: "+isbn2+" - "+"参考代码:"+this.n1+""+n2+""+g1+""+g2); System.out.println("流派:"+流派);返回 this.n1+""+n2+""+g1+""+g2+""+author+""+isbn2+""+title+""+genre;数字出现,但它打印的字符像这样#### empty square
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多