java - 如何获取输入并保存到数组中然后打印数组输入?

标签 java arrays

我正在尝试制作一个最多可容纳 25 本书作为输入的“书架”,打印出最新添加的书,然后如果用户想要打印这些书,它会打印出完整的数组,但我不知道该怎么做:/下面是我的代码,我还有另一个名为 Book 的类,它使用 toString 方法获取书籍信息,我试图让我的 printBooks 使用与我的 toString 方法相同的格式

package BookShelf;

import java.util.Scanner;
import java.util.Arrays;
  /**
   * this class adds a book object and adds them to a book shelf
   * @author H
   * @verison 9/30/2017
   */
  public class BookShelf{//class
  private Scanner scan;
  private String answer;
  public String line1;
  public String line2;
  public String line3;
  public String line4;
  public String line5;
  String[] book = new String[25];
  //book = new String[25];
  //private Arrays intName;
  private Boolean b;

/**
*constructor
*/
BookShelf(){ //constructor
  this.scan = new Scanner(System.in);
  line1 = " ";
  line2 = " ";
  line3 = " ";
  line4 = " ";
  line5 = " ";
  //this.book = new book;
}

/**
 * method that adds a book to the array
 * @return String, has new added book info in it
 */

public void addBook(){ //method
  //do{
  this.scan = new Scanner(System.in);
  System.out.println("Please enter the author's last name.");
  String line1 = scan.nextLine();
  System.out.println("Please enter the author's first name.");
  String line2 = scan.nextLine();
  System.out.println("Please enter the book title.");
  String line3 = scan.nextLine();
  System.out.println("Please enter the publisher.");
  String line4 = scan.nextLine();
  System.out.println("Please enter the publication year.");
  String line5 = scan.nextLine();
  System.out.println("Added book: " + line1 + ", " + line2 + " (" + line5 + ")" + ". " + line3 + ". " + line4);
  //} while ( b == false);{
      //for (int i = 0; i < book.length; i++){
        //if(book[i] == null){
          //this.b = true;
        //}
      //}
  //}
 //maybe make a counter to see if arrary is full and then do an if statement thats arr !full then ask questions

}


// Book[]library = newBook[n] //array


public void printBooks(){
  for( int i = 0; i < book.length; i++){
    System.out.println(book[i]);
  }
}

 //public void addedBookInfo(){
   //System.out.println(Arrays.toString(intName));
   //^this should work once arrays are figured out
//}


/**
* method that asks user for a command and then follows the command
* follows the command by calling other methods
*/
public void interact(){
  System.out.println("Would you like to add, print, or quit?");
  this.answer = scan.nextLine();
  if (answer.equals("add") || answer.equals("Add")){
    addBook();
  }
  else if (answer.equals("print") || answer.equals("Print")){
    printBooks();
  }
  else{
  }
}

/**
 * main method
 * @param rank valid values: args
 */
    public static void main(String[] args){ 
    BookShelf bs = new BookShelf();  //makes new shelf
    bs.interact(); //interacts with shelf
    }
}

最佳答案

您的实现中有一些看起来有点麻烦的东西,但我认为这段代码可能会满足您的要求:

package BookShelf;

import java.util.Scanner;
import java.util.Arrays;
  /**
   * this class adds a book object and adds them to a book shelf
   * @author H
   * @verison 9/30/2017
   */
  public class BookShelf {
  Scanner scan;
  static final int MAX_SIZE = 25;
  int index;
  String[] books;

  public BookShelf() {
      //In the constructor, initialize the array and the index.
      //it's best practice not to initialize resources like scanner 
      //you may forget to close them.
      books = new String[MAX_SIZE];
      index = 0;
  }
/**
 * method that adds a book to the array
 * @return String, has new added book info in it
 */

public void addBook() {
  //Check you have room for the book you want to add.
  if (index == MAX_SIZE) {
      System.out.println("There are "+MAX_SIZE+" in the book shelf");
  } else {
      this.scan = new Scanner(System.in);
      System.out.println("Please enter the author's last name.");
      String line1 = scan.nextLine();
      System.out.println("Please enter the author's first name.");
      String line2 = scan.nextLine();
      System.out.println("Please enter the book title.");
      String line3 = scan.nextLine();
      System.out.println("Please enter the publisher.");
      String line4 = scan.nextLine();
      System.out.println("Please enter the publication year.");
      String line5 = scan.nextLine();
      String book =  line1 + ", " + line2 + " (" + line5 + ")" + ". " + line3 + ". " + line4;
      System.out.println("Added book: " + book);
      books[index] = book;
      index++;

      //Close the scanner
      scan.close();
  }
}  

public void printBooks() {
  for( int i = 0; i < books.length; i++){
    System.out.println(books[i]);
  }
}

/**
* method that asks user for a command and then follows the command
* follows the command by calling other methods
*/
public void interact(){
  Scanner scan = new Scanner(System.in);
  String answer = scan.nextLine();
  while (!answer.equalsIgnoreCase("quit")) {
    System.out.println("Would you like to add, print, or quit?");
    answer = scan.nextLine();
    if (answer.equalsIgnoreCase("add")) {
      addBook();
    }
    else if (answer.equalsIgnoreCase("print")) {
      printBooks();
    }
  }
  scan.close();
}

/**
 * main method
 * @param rank valid values: args
 */
    public static void main(String[] args){ 
      BookShelf bs = new BookShelf();  //makes new shelf
      bs.interact(); //interacts with shelf
    }
}

关于java - 如何获取输入并保存到数组中然后打印数组输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515392/

相关文章:

java - 由于 Kotlin 中未解析的引用,无法设置文本或可绘制对象

java - 如何在普通 Java 类中找到上下文路径

JavaScript 数组迭代 - MDN 示例

c++ - 让一个类的实例搜索另一个类中的数组

c# - 在C#中对由整数组成的多维[,]数组进行排序

arrays - 将 Int 数组转换为逗号分隔的字符串

java - 如何从 JPanel 触发 ActionEvent

Java Comparator会计算1次或多次分数

ios - 符合协议(protocol)的 Swift 结构类型数组

java - 使用 Vagrant 设置 Java 开发环境时出错