繁体   English   中英

在toString方法Java中调用其他类时遇到麻烦

[英]Trouble calling other class in toString method Java

我对此感到愚蠢,但在上课的最后部分遇到了麻烦。 我似乎无法弄清楚要在两个字符串的最后一段中放入什么。 应该输入书名,出版商名和书评。 如果没有审查,它应该打印0.00(0),但我一直返回null。 任何帮助将非常感激。

书类

import java.text.DecimalFormat;
import java.util.*;

public class Book {

private String title;
private String publisher;
private Review bookReview;
DecimalFormat format = new DecimalFormat("0.00");

public Book(String title, String publisher, Review bookRev) {

    this.title = title;
    this.publisher = publisher;
    bookReview = bookRev;
}

public Book() {

    title = "?";
    publisher = "?";
    Review rev = new Review();
}

public String getTitle() {
    return title;
}
public String getPublisher() {
    return publisher;
}
public Review getReview() {
    return bookReview;
}
public void setTitle(String aTitle) {
    title = aTitle;
}
public void setPublisher(String aPublisher) {
    publisher = aPublisher;
}
public void addRating(double rate) {
    Review rev = new Review();
    rev.updateRating(rate);
}
public String toString() {
    return "\n" + "Title:" + "\t" + title + "," + "\n" + 
            "Publisher:" + "\t" + publisher + "," + "\n" +
            "Reviews:" + "\t" + bookReview + "\n\n";
}
}

复习班

public class Review {

private int numberOfReviews;
private double sumOfRatings;
private double average;
DecimalFormat format = new DecimalFormat("0.00");

public Review(int numRev, double sumRat, double average) {

    numberOfReviews = numRev;
    sumOfRatings = sumRat;
    this.average = average;
}

public Review() {

    numberOfReviews = 0;
    sumOfRatings = 0.0;
    average = 0.0;
}

public void updateRating(double rating) {

    numberOfReviews ++;
    sumOfRatings = sumOfRatings + rating;
    if (numberOfReviews > 0) {
        average = sumOfRatings / numberOfReviews;
    }
}

public String toString() {

    return "Reviews:" + "\t" + format.format(average) + "(" + numberOfReviews + ")";
}
}

import java.io.*;
import java.util.*;

主班

public class Assignment4
{
public static void main (String[] args)
{
   // local variables, can be accessed anywhere from the main method
   char input1 = 'Z';
   String inputInfo;
   String bookTitle;
   String bookPublisher;
   double rating;
   String line = new String();

   // instantiate a Book object
   Book book1 = new Book();

   printMenu();

   //Create a Scanner object to read user input
   Scanner scan = new Scanner(System.in);


   do  // will ask for user input
    {
     System.out.println("What action would you like to perform?");
     line = scan.nextLine();

     if (line.length() == 1)
      {
       input1 = line.charAt(0);
       input1 = Character.toUpperCase(input1);

       // matches one of the case statement
       switch (input1)
        {
         case 'A':   //Add Book
           book1 = new Book();
           System.out.print("Please enter the book information:\n");
           System.out.print("Enter a book title:\n");
           bookTitle = scan.nextLine();
           book1.setTitle(bookTitle);

           System.out.print("Enter its publisher:\n");
           bookPublisher = scan.nextLine();
           book1.setPublisher(bookPublisher);
           break;
         case 'D':   //Display Book
           System.out.print(book1);
           break;
         case 'Q':   //Quit
           break;
         case 'R':   //Add Rating
           System.out.print("Please give a rating of the book:\n");
           rating = Double.parseDouble(scan.nextLine());
           book1.addRating(rating);
           break;
         case '?':   //Display Menu
           printMenu();
           break;
         default:
           System.out.print("Unknown action\n");
           break;
        }
      }
     else
      {
       System.out.print("Unknown action\n");
      }
    } while (input1 != 'Q' || line.length() != 1);
  }


 /** The method printMenu displays the menu to a user**/
public static void printMenu()
{
 System.out.print("Choice\t\tAction\n" +
                    "------\t\t------\n" +
                    "A\t\tAdd Book\n" +
                    "D\t\tDisplay Book\n" +
                    "Q\t\tQuit\n" +
                    "R\t\tGive Rating\n" +
                    "?\t\tDisplay Help\n\n");
  }
 }

这是罪魁祸首

public void addRating(double rate) {
    Review rev = new Review();
    rev.updateRating(rate);
}

您正在做的是创建一个新对象,每当您添加评级时,该方法都会在方法调用后死亡,将其添加到您的实例变量而不是局部变量中,即像这样

 public void addRating(double rate) {
        bookReview = new Review();
        bookReview.updateRating(rate);
    }

由于这个原因,您的reviewBook将会更新并需要打印,结果

<---新编辑--->

查看您的Book类的默认构造函数

您正在通过使用Review rev = new Review();来创建新对象Review rev = new Review(); 但实际上应该是bookReview = new Review(); 您正在创建一个新对象,但是在toString只有bookReview可以访问,因此您需要对其进行初始化。

您必须初始化“ Review”对象。

我只是使用一些示例数据来初始化Book和Review:

Review reviewObj = new Review(23, 5, 133);
Mainclass obj  = new Mainclass("first things first", "s.covey", reviewObj);

下面是输出:

Title:  first things first,
Publisher:  s.covey,
Reviews:    Reviews:    133.00(23)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM