Java 如何获取输入并保存到数组中,然后打印数组输入?
我正在尝试制作一个“书架”,最多可以输入25本书,打印出最新添加的书,然后如果用户想要打印出这些书,它会打印出完整的数组,但我不知道如何操作:/下面是我的代码,我还有另一个名为Book的类,它使用toString方法来获取书籍信息,我正试图让我的打印本使用与我的toString方法相同的格式Java 如何获取输入并保存到数组中,然后打印数组输入?,java,arrays,Java,Arrays,我正在尝试制作一个“书架”,最多可以输入25本书,打印出最新添加的书,然后如果用户想要打印出这些书,它会打印出完整的数组,但我不知道如何操作:/下面是我的代码,我还有另一个名为Book的类,它使用toString方法来获取书籍信息,我正试图让我的打印本使用与我的toString方法相同的格式 package BookShelf; import java.util.Scanner; import java.util.Arrays; /** * this class adds a boo
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
}
}
包装书架;
导入java.util.Scanner;
导入java.util.array;
/**
*此类添加book对象并将其添加到书架
*@H作者
*@verison 9/30/2017
*/
公共类书架{//class
私人扫描仪扫描;
私有字符串应答;
公共字符串行1;
公共字符串行2;
公共字符串行3;
公共字符串行4;
公共字符串行5;
String[]book=新字符串[25];
//book=新字符串[25];
//私有数组名;
私有布尔b;
/**
*建造师
*/
书架(){//constructor
this.scan=新扫描仪(System.in);
第1行“”;
第2行“”;
第3行“”;
第4行“”;
第5行“”;
//这本书=新书;
}
/**
*方法将书本添加到数组中
*@return String,新增图书信息
*/
public void addBook(){//method
//做{
this.scan=新扫描仪(System.in);
System.out.println(“请输入作者的姓氏”);
字符串line1=scan.nextLine();
System.out.println(“请输入作者的名字”);
字符串line2=scan.nextLine();
System.out.println(“请输入书名”);
字符串line3=scan.nextLine();
System.out.println(“请输入发布者”);
字符串line4=scan.nextLine();
System.out.println(“请输入出版年份”);
字符串line5=scan.nextLine();
System.out.println(“添加的书籍:“+line1+”、“+line2+”(“+line5+”)“+”+line3+”+line4);
//}而(b==false){
//for(int i=0;i
您的实现中有一些东西看起来有点麻烦,但我认为这段代码可能会满足您的需求:
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.util.Scanner;
导入java.util.array;
/**
*此类添加book对象并将其添加到书架
*@H作者
*@verison 9/30/2017
*/
公共类书架{
扫描仪扫描;
静态最终整数最大值=25;
整数指数;
串[]本书;
公共书架(){
//在构造函数中,初始化数组和索引。
//最好的做法是不要初始化扫描器之类的资源
//你可能忘了关上它们。
books=新字符串[最大大小];
指数=0;
}
/**
*方法将书本添加到数组中
*@return String,新增图书信息
*/
公共无效地址簿(){
//检查您是否有空间放置要添加的书。
如果(索引==最大大小){
System.out.println(“书架上有“+MAX_SIZE+”);
}否则{
this.scan=新扫描仪(System.in);
System.out.println(“请输入作者的姓氏”);
字符串line1=scan.nextLine();
System.out.println(“请输入作者的名字”);
字符串line2=scan.nextLine();
System.out.println(“请输入书名”);
字符串line3=scan.nextLine();
System.out.println(“请输入发布者”);
字符串line4=scan.nextLine();
System.out.println(“请输入出版年份”);
字符串line5=scan.nextLine();
字串簿=第1行+“,“+line2+”(“+line5+”)“+”+“+line3+”+line4;
System.out.println(“添加的书本:+书本”);
图书[索引]=图书;
索引++;
//关闭扫描仪
scan.close();
}
}
公共印刷书籍(){
for(int i=0;i
不相关,但包装应命名为betteruse Collections在一个完全不相关的注释上,我建议减少answer.equals(“add”)| | answer.equals(“add”)
到answer.equalsIgnoreCase(“add”)
谢谢你的建议,但我刚刚更改了代码,现在我已经成功了