无法遍历java中的代码块

时间:2015-05-11 07:16:45

标签: java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class Book {

Scanner input = new Scanner(System.in);

public static void main(String args[]){
    Book b = new Book();
    System.out.println("********* Library Management System *********");
    b.addBooks();

}

public Book() {
    // TODO Auto-generated constructor stub
}

public void addBooks(){
    int x;
    int y =0;
    System.out.println("Enter 1 to add new book, 2 to View and 3 to exit: ");
    x = input.nextInt();
    input.nextLine();
    HashSet<Books> Library = new HashSet<Books>();

        if(x == 1){
            do {

                System.out.println("Enter the name of the book: ");
                String tempTitle = input.nextLine();

                System.out.println("Enter name of the Author: ");
                String tempAuthor = input.nextLine();

                System.out.println("Enter year of publishing the book: ");
                int tempYear = input.nextInt();

                System.out.println("Enter the Price: ");
                double tempPrice = input.nextDouble();


                Library.add(new Books(tempTitle, tempAuthor,  tempYear, tempPrice));



                System.out.println("Following details added to the database: ");

                Iterator itr=Library.iterator();  
                while(itr.hasNext()){  
                    Books b=(Books)itr.next();  
                    System.out.println("\n Title: " + b.title + " \n Author: "+b.author + "\n Publish Year: " +b.publishYear + "Price: " + b.price);  
                }  


                System.out.println("Do you want to add another book? 1. Yes 2. No");
                y = input.nextInt();
                input.nextLine();
            } while (y == 1);




    } else if (x == 2){

        Iterator itr=Library.iterator();  
        while(itr.hasNext()){  
            Books b=(Books)itr.next();  
            System.out.println("\n Title: " + b.title + " \n Author: "+b.author + "\n Publish Year: " +b.publishYear + "Price: " + b.price);  


        } 


    }
}

我正在制作图书馆/书店计划以学习基础知识。 我现在坚持的问题是, 通过addBooks()方法添加我想要的书后,程序会询问用户是否要添加另一本书。如果用户回答YES,即1,则它再次循环通过相同的代码。 但是如果用户输入NO,即2,程序在进入主类后停止。 我想再次向用户询问之前被问过的三个选项:
输入1以添加新书,输入2以查看,输入3以退出:
当用户选择2时,他们会获得他们刚才添加的所有细节。如果他们选择1,他们再次有机会添加新书。我如何再次循环该部分?

2 个答案:

答案 0 :(得分:1)

尝试将循环移回一个级别 - 这也会为您提供更清晰的代码。在伪代码中,它看起来像这样。

public static void main(String args[]){
    boolean doLoop = true;
    while (doLoop) {
        int choice = input.nextInt();
        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                listBooks();
                break;
            case 3:
                doLoop = false;
                break;
            default:
                System.out.println("Please enter a valid choice");
        }
    }

}

答案 1 :(得分:1)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class Books {

Scanner input = new Scanner(System.in);

String title;
String author;
int publishYear;
double price;

public Books(String tempTitle, String tempAuthor, int tempYear, double tempPrice) {
    title = tempTitle;
    author = tempAuthor;
    publishYear = tempYear;
    price = tempPrice;
}

public static void main(String args[]) {
    Books b = new Books();
    System.out.println("********* Library Management System *********");
    b.addBooks();

}

public Books() {
    // TODO Auto-generated constructor stub
}

public void addBooks() {
    int x;
    int y = 0;


    HashSet<Books> Library = new HashSet<Books>();


    do {
        System.out.println("Enter 1 to add new book, 2 to View and 3 to exit: ");
        x = input.nextInt();
        input.nextLine();

        if (x == 1) {
            System.out.println("Enter the name of the book: ");
            String tempTitle = input.nextLine();

            System.out.println("Enter name of the Author: ");
            String tempAuthor = input.nextLine();

            System.out.println("Enter year of publishing the book: ");
            int tempYear = input.nextInt();

            System.out.println("Enter the Price: ");
            double tempPrice = input.nextDouble();


            Library.add(new Books(tempTitle, tempAuthor, tempYear, tempPrice));


            System.out.println("Following details added to the database: ");

            Iterator itr = Library.iterator();
            while (itr.hasNext()) {
                Books b = (Books) itr.next();
                System.out.println("\n Title: " + b.title + " \n Author: " + b.author + "\n Publish Year: " + b.publishYear + "Price: " + b.price);
            }
        } else if (x == 2) {

            Iterator itr = Library.iterator();
            while (itr.hasNext()) {
                Books b = (Books) itr.next();
                System.out.println("\n Title: " + b.title + " \n Author: " + b.author + "\n Publish Year: " + b.publishYear + "Price: " + b.price);

            }
        }
    }while (x == 1 || x == 2) ;
}

}

此代码只需极少的更改即可满足您的需求。

我选择使用带枚举的开关而不是if else块。这也更容易阅读和维护。