简体   繁体   English

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

[英]How to compare sales from an array and get the highest and lowest selling

Does anybody have the code to be able to be able to compare the sales, or a variable, from an array, and be able to print out the highest and lowest selling items. 是否有人拥有能够比较阵列中的销售额或变量的代码,并能够打印出最高和最低销售额的代码。 I have tried several things but cant exactly get it to work. 我已经尝试了几种方法,但无法完全使其正常工作。

Here is my code, the stuff I am most concerned about is at the bottom: 这是我的代码,我最关心的事情在底部:

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;
        }
    }
}
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 will now contain the highest selling album, assuming that your Album class has a proper getSales() method that returns the sales. highestSellingAlbum现在将包含销量最高的专辑,假设您的Album类具有正确的getSales()方法可返回销售。

to get lowestSales ever set you need to initialize it with a higher value as the real lowest that you look for. 要获得有史以来的最低lowestSales设置,您需要使用更高的值将其初始化为您所寻找的实际最低价格。 that could be Double.MAX_VALUE or the highestSale that you found before (assuming you fix the bug). 可能是Double.MAX_VALUE或您之前找到的最高销售(假设您已修复错误)。 You also should initialize highestSale of course, maybe with 0.0. 当然,您还应该初始化highestSale,也许用0.0初始化。

The fix for highestSale would be to assign highestSales = i.getSales() in your loop; mostSale的解决方法是在循环中分配highestSales = i.getSales()

here is a full and working version: 这是完整且有效的版本:

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

    }

}

There were numerous errors in your posted version, including compiling errors. 您发布的版本中存在许多错误,包括编译错误。 You should start using an IDE like eclipse for your java experiments, because such environments show you your errors while programming. 您应该开始在Java实验中使用像eclipse这样的IDE,因为这样的环境会在编程时向您显示错误。

To the algorithmic problem of yours. 对您的算法问题。 The key point is, that you need to initialize the variables against which you compare. 关键是,您需要初始化要比较的变量。 I guess that is what you tried with that line ((Comparable<String>) albums[0]).compareTo(albums[1]); 我想这就是您尝试的那行((Comparable<String>) albums[0]).compareTo(albums[1]); in your code? 在您的代码中? Well, that line made no sense. 好吧,那条线没有意义。 Also, you were mixing the index of the array of Albums with the sales value of the albums. 另外,您正在将“专辑”数组的索引与专辑的销售额混合在一起。 Well, have a look at my solution and study it. 好吧,看看我的解决方案并进行研究。 In future, please post only code that at least has no compilation errors. 将来,请仅发布至少没有编译错误的代码。

You can Implement Comparable Interface in Album class if you can change it and the simply call Arrays.sort(albums) , it will short your array you can use first and last element of array as lowest and highest selling albums respectively. 如果可以更改相册类,则可以实现可比较接口,并且只需调用Arrays.sort(albums) ,它将使您的数组变短,您可以将数组的第一个和最后一个元素分别用作销量最低和销量最高的专辑。


Code will be some thing like this 代码将是这样的

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



If you can't change Album class then Grammin's answer is ok. 如果您不能更改专辑的类别,那么格拉明的答案就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM