下面是我的 代码
import java.util.Scanner;
public class BookTest {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
int n;
System.out.println("How many books do you want to input");
n = keyboard.nextInt();
Book[] books = new Book[n];
for(int i = 0; i < n; i++){
books[i] = new Book();
System.out.println("Please enter the title for book "+(i+1)+" :");
books[i].title = keyboard.nextLine();//这里用nextline 每次运行都会被跳过 如果只用next 就不能输入类似New York 这种带空格的string
System.out.println("Please enter the author name");
books[i].author = keyboard.nextLine();
do{
System.out.println("please enter the number of pages");
books[i].numberOfPadge = keyboard.nextInt();
}while(books[i].numberOfPadge < 1);
}
System.out.println("Book titles:");
for (int i = 0; i < n; i++){
System.out.println((i+1)+". "+books[i].title);
}
}
}
import java.util.Scanner;
public class BookTest {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
int n;
System.out.println("How many books do you want to input");
n = keyboard.nextInt();
Book[] books = new Book[n];
for(int i = 0; i < n; i++){
books[i] = new Book();
System.out.println("Please enter the title for book "+(i+1)+" :");
books[i].title = keyboard.nextLine();//这里用nextline 每次运行都会被跳过 如果只用next 就不能输入类似New York 这种带空格的string
System.out.println("Please enter the author name");
books[i].author = keyboard.nextLine();
do{
System.out.println("please enter the number of pages");
books[i].numberOfPadge = keyboard.nextInt();
}while(books[i].numberOfPadge < 1);
}
System.out.println("Book titles:");
for (int i = 0; i < n; i++){
System.out.println((i+1)+". "+books[i].title);
}
}
}