Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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 查找ArrayList中元素的百分比<;模型>;_Java_Arraylist - Fatal编程技术网

Java 查找ArrayList中元素的百分比<;模型>;

Java 查找ArrayList中元素的百分比<;模型>;,java,arraylist,Java,Arraylist,因此,我正在做一个项目,我想用两个ArrayList构建一个库ArrayList“”中的一个BookList包含一个名为quantity的元素,如果书籍的数量大于零,则该元素必须大于或等于零如果书籍的数量大于零,则BookList中另一个名为status的元素设置为in stock,如果它等于零,则将其设置为borrowed。我正在尝试创建一个方法来查看图书列表并显示借阅图书的百分比。我通过查看列表完成了这项工作,每次它发现一本书的数量低于1,换句话说,0,计数器就会上升1,所以最后我只是从Bo

因此,我正在做一个项目,我想用两个ArrayList构建一个库ArrayList“”中的一个BookList包含一个名为quantity的元素,如果书籍的数量大于零,则该元素必须大于或等于零如果书籍的数量大于零,则BookList中另一个名为status的元素设置为in stock,如果它等于零,则将其设置为borrowed。我正在尝试创建一个方法来查看图书列表并显示借阅图书的百分比。我通过查看列表完成了这项工作,每次它发现一本书的数量低于1,换句话说,0,计数器就会上升1,所以最后我只是从BookList.size()中减去计数器,将结果除以BookList.size(),将其乘以100,然后最终打印出来

主类

public class Main {
    public static void main(String[] args) {
    Scanner keyb = new Scanner(System.in);
    int uinput;
    Library nag;
    try{
    nag = new Library();
    do{
        System.out.println("Type 1 to add a book.");
        System.out.println("Type 2 to show how many books are borrowed.");
        uinput = keyb.nextInt();
        if (uinput==1){
            nag.addBook();
        }
        if (uinput==2){
            nag.statistics();
        }
    }while (uinput > 0);
    }
    catch(Exception e){
        System.out.println("Invalid entry.");
    }
}//end of main
}//end of class
图书班

public class Book {

    private String Title;
    private String Author;
    private String ISBN;
    private String Publisher;
    private String Publication_Date;
    private String Price;
    private int Quantity;
    private String Status;

    public Book(){
        Title= "";
        Author="";
        ISBN="";
        Publisher="";
        Publication_Date="";
        Price="";
        Quantity=1;
        Status="IN-STOCK";
    }

    //getters
    public String gettitle(){return Title;}
    public String getauthor(){return Author;}
    public String getisbn(){return ISBN;}
    public String getpublisher(){return Publisher;}
    public String getpublication_date(){return Publication_Date;}
    public String getprice(){return Price;}
    public int getquantity(){return Quantity;}
    public String getstatus(){return Status;}

    //setters
    public void settitle(String t){Title = t;}
    public void setauthor(String a){Author = a;}
    public void setisbn(String is){ISBN = is;}
    public void setpublisher(String p){Publisher = p;}
    public void setpublication_date(String pd){Publication_Date = pd;}
    public void setprice(String pr){Price = pr;}
    public void setquantity(int q){Quantity = q;}
    public void setstatus(String s){Status = s;}

}//end of class
图书馆课

public class Library {

private ArrayList<Book> BookList;

public Library(){
    BookList = new ArrayList<Book>();
}//end of constructor 1

public Library(ArrayList<Book> l) {
    BookList = l;
}//end of constructor 3

public void addBook(){
    try{
        Scanner keyb = new Scanner(System.in);
        Book bo = new Book();

        System.out.println("Type the title: ");
        String title_input;
        title_input = keyb.nextLine();
        bo.settitle(title_input);

        System.out.println("Type the author: ");
        String author_input;
        author_input = keyb.nextLine();
        bo.setauthor(author_input);

        System.out.println("Type the isbn: ");
        String isbn_input;
        isbn_input = keyb.nextLine();
        bo.setisbn(isbn_input);

        System.out.println("Type the publisher: ");
        String publisher_input;
        publisher_input = keyb.nextLine();
        bo.setpublisher(publisher_input);

        System.out.println("Type the publication date: ");
        String publication_date_input;
        publication_date_input = keyb.nextLine();
        bo.setpublication_date(publication_date_input);

        System.out.println("Type the price: ");
        String price_input;
        price_input = keyb.nextLine();
        bo.setprice(price_input);

        System.out.println("Type the quantity: ");
        int quantity_input = Integer.parseInt(keyb.nextLine());
        if (quantity_input >= 0){
            bo.setquantity(quantity_input);
            if (quantity_input > 0){
                bo.setstatus("IN_STOCK");
            }
            if(quantity_input == 0){
                bo.setstatus("BORROWED");
            }
            BookList.add(bo);
            System.out.println("Book added successfully.");
        }
    }catch(Exception e){
        System.out.println("Invalid entry");
    }//end of addBook() 

public void statistics(){
    Book bo = new Book();
    int counter = 0;
    for(int i=0; i < BookList.size();i++){
        bo= BookList.get(i);
        int holdquantity = bo.getquantity();
        if (holdquantity  < 1){
            counter++;
        }  
    }
    double substraction=BookList.size() - counter;
    double division= substraction/BookList.size();
    double percentage = division * 100;
    System.out.print(percentage + "%");
}//end of statistics()
}//end of class
公共类库{
私人ArrayList书目;
公共图书馆(){
BookList=newarraylist();
}//构造函数1结束
公共图书馆(ArrayList l){
BookList=l;
}//构造函数3结束
公共无效地址簿(){
试一试{
扫描仪keyb=新扫描仪(System.in);
Book bo=新书();
System.out.println(“键入标题:”);
字符串标题输入;
title_input=keyb.nextLine();
bo.settitle(标题输入);
System.out.println(“键入作者:”);
字符串作者输入;
author_input=keyb.nextLine();
设置作者(作者输入);
System.out.println(“键入isbn:”);
字符串isbn_输入;
isbn_输入=keyb.nextLine();
设置isbn(isbn_输入);
System.out.println(“键入发布者:”);
字符串输入;
publisher_input=keyb.nextLine();
bo.setpublisher(publisher\u输入);
System.out.println(“键入发布日期:”);
字符串发布日期输入;
publication_date_input=keyb.nextLine();
设置发布日期(发布日期输入);
System.out.println(“键入价格:”);
字符串价格输入;
price_input=keyb.nextLine();
bo.设定价格(价格输入);
System.out.println(“键入数量:”);
int quantity_input=Integer.parseInt(keyb.nextLine());
如果(数量输入>=0){
bo.设置数量(数量输入);
如果(数量输入>0){
bo.setstatus(“库存”);
}
如果(数量输入=0){
bo.setstatus(“借款”);
}
图书目录。添加(bo);
System.out.println(“图书添加成功”);
}
}捕获(例外e){
System.out.println(“无效条目”);
}//addBook()的结尾
公共空间统计(){
Book bo=新书();
int计数器=0;
对于(int i=0;i
问题是,当我有一本数量为零的书和另一本数量大于零的书时,它会保持100.0%的打印。
因此,我想知道问题是出在这段代码还是其他地方。

假设您的图书类如下所示:

package com.company;

public class Book {

    private int quantity;

    public Book(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}
package com.company;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main (String[]args) {

        List<Book> bookList = new ArrayList<>(){{
            add(new Book(4));
            add(new Book(0));
            add(new Book(3));
            add(new Book(7));
            add(new Book(0));
            add(new Book(0));
            add(new Book(1));
            add(new Book(9));
            add(new Book(0));
            add(new Book(5));
        }};

        int booksOutOfStock = 0;

        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getQuantity() == 0)
                booksOutOfStock++;
        }

        double percentage = 100d / bookList.size() * booksOutOfStock;

        System.out.printf("Out of total %d books, %d are out of stock, which makes %.2f%%", bookList.size(), booksOutOfStock,
                                                                                                        percentage);
    }
}
你的主要意见如下:

package com.company;

public class Book {

    private int quantity;

    public Book(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}
package com.company;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main (String[]args) {

        List<Book> bookList = new ArrayList<>(){{
            add(new Book(4));
            add(new Book(0));
            add(new Book(3));
            add(new Book(7));
            add(new Book(0));
            add(new Book(0));
            add(new Book(1));
            add(new Book(9));
            add(new Book(0));
            add(new Book(5));
        }};

        int booksOutOfStock = 0;

        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getQuantity() == 0)
                booksOutOfStock++;
        }

        double percentage = 100d / bookList.size() * booksOutOfStock;

        System.out.printf("Out of total %d books, %d are out of stock, which makes %.2f%%", bookList.size(), booksOutOfStock,
                                                                                                        percentage);
    }
}

你的for循环条件怎么了?for(int i=0;BookList.size();i++)我的坏朋友忘了在这里写,但我编辑了它。去接受编辑,你的for循环条件没有被转义,并且被更正了。你能发布你的所有代码吗,我会帮你解决这个问题。@Santosh是我