Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法循环使用java中的代码块 import java.util.ArrayList; 导入java.util.HashSet; 导入java.util.Iterator; 导入java.util.Scanner; 公共课堂用书{ 扫描仪输入=新扫描仪(System.in); 公共静态void main(字符串参数[]){ 书b=新书(); System.out.println(“**********图书馆管理系统**********”); b、 addBooks(); } 公共书籍(){ //TODO自动生成的构造函数存根 } 公共书籍{ int x; int y=0; System.out.println(“输入1添加新书,输入2查看,输入3退出:”; x=input.nextInt(); input.nextLine(); HashSet Library=新的HashSet(); 如果(x==1){ 做{ System.out.println(“输入图书名称:”); 字符串TENTITLE=input.nextLine(); System.out.println(“输入作者姓名:”); 字符串tempAuthor=input.nextLine(); System.out.println(“输入图书出版年份:”); int tempYear=input.nextInt(); System.out.println(“输入价格:”); double tempPrice=input.nextDouble(); 添加(新书(Tentitle、tempAuthor、tempYear、tempPrice)); System.out.println(“以下详细信息添加到数据库:”); 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } System.out.println(“是否要添加另一本书?1.是2.否”); y=输入。nextInt(); input.nextLine(); }而(y==1); }else如果(x==2){ 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } } }_Java - Fatal编程技术网

无法循环使用java中的代码块 import java.util.ArrayList; 导入java.util.HashSet; 导入java.util.Iterator; 导入java.util.Scanner; 公共课堂用书{ 扫描仪输入=新扫描仪(System.in); 公共静态void main(字符串参数[]){ 书b=新书(); System.out.println(“**********图书馆管理系统**********”); b、 addBooks(); } 公共书籍(){ //TODO自动生成的构造函数存根 } 公共书籍{ int x; int y=0; System.out.println(“输入1添加新书,输入2查看,输入3退出:”; x=input.nextInt(); input.nextLine(); HashSet Library=新的HashSet(); 如果(x==1){ 做{ System.out.println(“输入图书名称:”); 字符串TENTITLE=input.nextLine(); System.out.println(“输入作者姓名:”); 字符串tempAuthor=input.nextLine(); System.out.println(“输入图书出版年份:”); int tempYear=input.nextInt(); System.out.println(“输入价格:”); double tempPrice=input.nextDouble(); 添加(新书(Tentitle、tempAuthor、tempYear、tempPrice)); System.out.println(“以下详细信息添加到数据库:”); 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } System.out.println(“是否要添加另一本书?1.是2.否”); y=输入。nextInt(); input.nextLine(); }而(y==1); }else如果(x==2){ 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } } }

无法循环使用java中的代码块 import java.util.ArrayList; 导入java.util.HashSet; 导入java.util.Iterator; 导入java.util.Scanner; 公共课堂用书{ 扫描仪输入=新扫描仪(System.in); 公共静态void main(字符串参数[]){ 书b=新书(); System.out.println(“**********图书馆管理系统**********”); b、 addBooks(); } 公共书籍(){ //TODO自动生成的构造函数存根 } 公共书籍{ int x; int y=0; System.out.println(“输入1添加新书,输入2查看,输入3退出:”; x=input.nextInt(); input.nextLine(); HashSet Library=新的HashSet(); 如果(x==1){ 做{ System.out.println(“输入图书名称:”); 字符串TENTITLE=input.nextLine(); System.out.println(“输入作者姓名:”); 字符串tempAuthor=input.nextLine(); System.out.println(“输入图书出版年份:”); int tempYear=input.nextInt(); System.out.println(“输入价格:”); double tempPrice=input.nextDouble(); 添加(新书(Tentitle、tempAuthor、tempYear、tempPrice)); System.out.println(“以下详细信息添加到数据库:”); 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } System.out.println(“是否要添加另一本书?1.是2.否”); y=输入。nextInt(); input.nextLine(); }而(y==1); }else如果(x==2){ 迭代器itr=Library.Iterator(); 而(itr.hasNext()){ 图书b=(图书)itr.next(); System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”); } } },java,Java,我正在制作图书馆/书店程序来学习基础知识。 我现在面临的问题是, 通过addBooks()方法添加我想要的书后,程序会询问用户是否要添加另一本书。如果用户回答是,即1,则程序会再次循环相同的代码。 但是如果用户输入NO.e.,2,程序在进入主类后停止。 我想再次向用户询问之前询问的三个选项: “输入1添加新书,输入2查看,输入3退出:” 当用户选择2时,他们将获得刚才添加的所有详细信息。如果选择1,他们将再次获得添加新书的机会。如何再次循环该部分?尝试将循环移回一个级别-这也将为您提供更清晰的代

我正在制作图书馆/书店程序来学习基础知识。 我现在面临的问题是, 通过addBooks()方法添加我想要的书后,程序会询问用户是否要添加另一本书。如果用户回答是,即1,则程序会再次循环相同的代码。 但是如果用户输入NO.e.,2,程序在进入主类后停止。 我想再次向用户询问之前询问的三个选项:
输入1添加新书,输入2查看,输入3退出:

当用户选择2时,他们将获得刚才添加的所有详细信息。如果选择1,他们将再次获得添加新书的机会。如何再次循环该部分?

尝试将循环移回一个级别-这也将为您提供更清晰的代码。在伪代码中,它将如下所示

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);  


        } 


    }
}
import java.util.ArrayList;
导入java.util.HashSet;
导入java.util.Iterator;
导入java.util.Scanner;
公共课用书{
扫描仪输入=新扫描仪(System.in);
字符串标题;
字符串作者;
国际出版年;
双倍价格;
公共图书(String-tentitle、String-tempAuthor、int-tempYear、double-tempPrice){
title=tentitle;
作者=临时作者;
publishYear=tempYear;
价格=临时价格;
}
公共静态void main(字符串参数[]){
图书b=新书();
System.out.println(“**********图书馆管理系统**********”);
b、 addBooks();
}
公共书籍(){
//TODO自动生成的构造函数存根
}
公共书籍{
int x;
int y=0;
HashSet Library=新的HashSet();
做{
System.out.println(“输入1添加新书,输入2查看,输入3退出:”;
x=input.nextInt();
input.nextLine();
如果(x==1){
System.out.println(“输入图书名称:”);
字符串TENTITLE=input.nextLine();
System.out.println(“输入作者姓名:”);
字符串tempAuthor=input.nextLine();
System.out.println(“输入图书出版年份:”);
int tempYear=input.nextInt();
System.out.println(“输入价格:”);
double tempPrice=input.nextDouble();
添加(新书(Tentitle、tempAuthor、tempYear、tempPrice));
System.out.println(“以下详细信息添加到数据库:”);
迭代器itr=Library.Iterator();
while(itr.hasNext()){
图书b=(图书)itr.next();
System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”);
}
}else如果(x==2){
迭代器itr=Library.Iterator();
while(itr.hasNext()){
图书b=(图书)itr.next();
System.out.println(“\n Title:+b.Title+”\n Author:+b.Author+”\n发布年份:+b.publishYear+“Price:+b.Price”);
}
}
}而(x==1 | | x==2);
}
}

此代码只需很少的更改即可满足您的需要


我选择将switch与enum一起使用,而不是if-else块。这也更易于阅读和维护。

尝试边执行边循环这不会有帮助。.我想在用户添加书籍后获得一个列表书籍选项,而不终止程序。这样,我可以选择1、2或3。但如果我想在添加后查看,则不能选择1和2。Re-r读取循环。程序只有在输入
3
时才会退出。否则,它将继续循环,您可以根据需要添加书籍或列出书籍。哦,是的,我的糟糕!谢谢!
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");
        }
    }

}
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) ;
}