java中填充数组的百分比

时间:2015-02-27 17:23:03

标签: java

在这个类中,如果数组tabletet填充75%,我需要 它必须是一个方法(readyPorosia)它被填充 任何更好的sugestions请

public class Porosia
{

    private String shifraPorosia;
    private int pozita;
    private Tableti [] tabletet;
/* */
    public Porosia(String sh, int nr)
    {

        tabletet = new Tableti[nr];
        shifraPorosia = sh;
    }

    public void addTablet(Tableti t)
    {
        if(t == null){
            System.out.println("Tablet can't be added");
        }else if(full()){
            System.out.println("Tablet can't be added.It's full");
        }else{
            tabletet[pozita++] = t;
            System.out.println("Tablet added");
        }
    }

    public boolean full()
    {
        if(pozita >= tabletet.length){
            return true;
        }
        return false;
    }

    public boolean readyPorosia(Tableti [] t)
    {
     ///in here to find the percentage of how much is filled the array
        if(percentage >= 75){
            return true;
        }
        return false;

    }

    public static void main(String[] args)
    {
        Porosia por = new Porosia("12122/2015",50);

        por.addTablet(new Tableti("Samsung",7));
        por.addTablet(new Tableti("Apple",10));
        por.addTablet(new Tableti("Dell",20));

        Tableti t4 = new Tableti("Lenovo",2);
        por.addTablet(t4);


        por.readyPorosia(por);
    }
}

1 个答案:

答案 0 :(得分:1)

如果您需要填充百分比,则100*pozita/tabletet.length。您可以将该数字与75进行比较。

public boolean readyPorosia(Tableti [] t)
{
    int percentage = 100*pozita/tabletet.length;
    if(percentage >= 75){
        return true;
    }
    return false;
}