我有一个我正在研究的方法,它是一个更大的程序的一部分,但我认为我发布的代码就足够了.
当我在菜单中选择选项1时,它可以正常工作,但是当我选择选项2时,它只是结束程序.任何人都可以发现这个问题吗?
解决:选择== 1应为2.
我还可以添加这个问题,最好将插入的数据放入数组中,如果是这样,我应该在主类,超类或Sub类中声明数组
static void addBook(){
String title,author;
int choice;
boolean onLoan;
loanbook book1; // TESTING ONLY
System.out.print("Press 1 for Fiction or 2 for Non Fiction: "); // sub menu for fiction and non fiction
choice = keyboard.nextInt();
if (choice == 1){
System.out.println("Please enter book title: ");
title = keyboard.nextLine();
title = keyboard.nextLine();
System.out.println("Please enter book author: ");
author = keyboard.nextLine();
onLoan = false; // not used yet
book1 = new fiction(title,author);
System.out.println(book1.toString());
}
else if (choice == 1) {
System.out.println("Please enter book title: ");
title = keyboard.nextLine();
title = keyboard.nextLine(); ;
System.out.println("Please enter book author: ");
author = keyboard.nextLine();
onLoan = false; // not used yet
book1 = new nonfiction(title,author);
System.out.println(book1.toString());
}
}