如何比较阵列的销售额并获得最高和最低销量

时间:2013-04-26 19:17:41

标签: java arrays compare

是否有人能够比较阵列中的销售额或变量,并能够打印出最高和最低销售商品。我已经尝试了几件事,但无法让它完全发挥作用。

这是我的代码,我最关心的是底层:

package album;

public class Certifier {

    public static void main(String[] args) {

Album zero = new Album(0, "Dark Side of the Moon", "Pink Floyd", 1973, 50000000);

        System.out.println("The album information for ablum 0 is: " + zero.toString());

        Album one = new Album(1, "Superficial", "Heidi Montag",  2010, 658);

        System.out.println("The album information for ablum 1 is: " + one.toString());

        Album two = new Album(2, "Fearless", "Taylor Swift", 2011, 3200000);

        System.out.println("The album information for ablum 2 is: " + two.toString());

        Album three = new Album(3, "Made Up", "Sample Band", 2011, 1300000);

        System.out.println("The album information for ablum 3 is: " + three.toString());

        Album four = new Album(4, "Concerto One Night", "Andrea Bocelli", 2011, 500008);

        System.out.println("The album information for ablum 4 is: " + four.toString());

        Album[] albums = new Album[4];
            albums[0] = zero;
            albums[1] = one;
            albums[2] = two;
            albums[3] = three;
            albums[4] = four;


            ((Comparable<String>) albums[0]).compareTo(albums[1]);

        System.out.println("The highest selling album is");
        for (int i=0; i< Album[].length; i++) {
            double highestSales;
            if (i.getSales()<highestSales)
                highestSales= i;
            return highestSales;

        }

    System.out.println("The lowest selling album is");
        for (int i=0; i< Album[].length; i++) {
            double lowestSales;
            if (i.getSales()<lowestSales)
                lowestSales= i.getSales();
            return i.getName + i.getArtist;
        }
    }
}

4 个答案:

答案 0 :(得分:0)

Album highestSellingAlbum;
double highestSales = 0;

for (int i=0; i< albums.length; i++) 
{
  if (albums[i].getSales() > highestSales)
  {
    highestSales = albums[i].getSales();
    highestSellingAlbum = albums[i];
  }
}

highestSellingAlbum现在将包含销售量最高的专辑,假设您的Album类具有返回销售的正确getSales()方法。

答案 1 :(得分:0)

要设置lowestSales,您需要使用更高的值将其初始化为您查找的最低值。可能是Double.MAX_VALUE或您之前找到的最高销售额(假设您修复了该错误)。你当然也应该初始化最高级别,当然也许是0.0。

highSale的修复方法是在循环中分配highestSales = i.getSales();

答案 2 :(得分:0)

这是一个完整且有效的版本:

package album;


public class Certifier {

    public static void main(String[] args) {

        Album zero = new Album(0, "Dark Side of the Moon", "Pink Floyd", 1973, 50000000);
        System.out.println("The album information for ablum 0 is: " + zero.toString());

        Album one = new Album(1, "Superficial", "Heidi Montag",  2010, 658);
        System.out.println("The album information for ablum 1 is: " + one.toString());

        Album two = new Album(2, "Fearless", "Taylor Swift", 2011, 3200000);
        System.out.println("The album information for ablum 2 is: " + two.toString());

        Album three = new Album(3, "Made Up", "Sample Band", 2011, 1300000);
        System.out.println("The album information for ablum 3 is: " + three.toString());

        Album four = new Album(4, "Concerto One Night", "Andrea Bocelli", 2011, 500008);
        System.out.println("The album information for ablum 4 is: " + four.toString());

        Album[] albums = new Album[5]; //we have 5 albums!
            albums[0] = zero;
            albums[1] = one;
            albums[2] = two;
            albums[3] = three;
            albums[4] = four;

        Album highestSellingAlbum = zero; //must be initialized with any of the 5
        Album lowestSellingAlbum = zero; //must be initialized with any of the 5

        for (int i=0; i< albums.length; i++) {

            if (albums[i].getSales() > highestSellingAlbum.getSales()){
                highestSellingAlbum = albums[i];
            }
            if (albums[i].getSales() < lowestSellingAlbum.getSales()){
                lowestSellingAlbum = albums[i];
            }
        }
        System.out.println("The highest selling album is " + highestSellingAlbum);
        System.out.println("The highest sales is " + highestSellingAlbum.getSales() ); 

        System.out.println("The lowest selling album is " + lowestSellingAlbum);
        System.out.println("The lowest sales is " + lowestSellingAlbum.getSales() );

    }

}

您发布的版本中存在大量错误,包括编译错误。您应该开始使用像eclipse这样的IDE来进行Java实验,因为这样的环境会在编程时向您显示错误。

对你的算法问题。关键是,您需要初始化您比较的变量。我想您在代码中使用该行((Comparable<String>) albums[0]).compareTo(albums[1]);尝试的是什么?好吧,这条线毫无意义。此外,您正在将Albums数组的索引与相册的销售值混合。那么,看看我的解决方案并研究它。将来,请仅发布至少没有编译错误的代码。

答案 3 :(得分:-1)

你可以在Album类中实现Comparable Interface,如果你可以改变它并简单地调用Arrays.sort(albums),它会缩短你的数组,你可以先使用数组的最后一个元素作为最低和最高销售的专辑。


代码将是这样的事情

public class Album implements Comparable<Album>{
     /*
      your existing code here
     */
     public int compareTo(Album compareAlbum) {
       int compareSales = ((Album) compareAlbum).getSales(); 
       return this.sales - compareSales
     }
}



如果您无法更改专辑类,那么Grammin的答案就可以了。