0

我有六个任务需要完成,但我不知道如何显示它们。我有所有代码来计算所需的每项任务,但是在将它们应用于我的测试器类时,我被卡住了。下面是我的原始类代码。这些是需要完成的任务:

  1. 显示原始数据集,如表所示。
  2. 连锁超市的平均利润。
  3. 利润最高的城市。
  4. 利润达到或高于平均水平的所有城市的列表。
  5. 城市及其利润按利润降序排列。
  6. 制作水平图表显示每个超市的表现

请帮忙,因为过去几天我一直在这样做,但仍然无法做任何事情......我一定很愚蠢,请帮忙

package supermarkets;

public class Supermarkets 
{

    private String[] city = {"Miami", "Sunrise", "Hollywood",
        "Tallahassee", "Jacksonville", "Orlando", "Gainesville", "Pensacola",
        "Ocala", "Sebring"};

    private double[] profit = {10200000, 14600000, 17000000, 6000000,
        21600000, 9100000, 8000000, 12500000, 2000000, 4500000};

    public Supermarkets(String[] c, double[] p)
    {
        //array for the city
        city = new String[c.length];
        for (int i = 0; i < c.length; i++)
            city[i] = c[i];


        //array for the profits
        profit = new double[p.length];
        for(int i = 0; i < p.length; i++)
            profit[i] = p[i];
    }


    //sums up the profits from the cities
    public double sumArray()
    {
        double sum = 0;

        for (int i = 0; i < profit.length; i++)
            sum = sum + profit [i];

        return sum;
    }

    //calculates the average of the profits from the cities
    public double average()
    {
        return sumArray() / profit.length;
    }


    //shows highest profit
    double findHighestProfit() 
    {
        double highest = profit[0];

        for(int i = 1; i < profit.length; i++)
        {
            if ( profit [i] > highest )
                highest = profit [i];      
        }
        return highest;
    }


    //gives the above average profit
    public String aboveAvarage()
    {
        String s = "";
        double avg = average();
        for (int i = 0; i < profit.length; i++)
            if (profit [i] > avg)
                s = s + city[i] + "  " + profit[i] + "\n";
        return s;
    }


    //creates a graph showing city and profits
    public String makeGraph()
    {
        String s = "";
        for (int i = 0; i < profit.length; i++)
        {
            s = s + city[i] + " ";
            int x = (int) Math.floor( profit[i] );

            for(int j = 1; j <=x; j++)
                s = s + "*";
            s = s + "\n";
        }
        return s;
    }


    //resets profits position from least to greatest
    public int findPosition(int startScanFrom)
    {
        int position = startScanFrom;

        for (int i = startScanFrom + 1; i < profit.length; i++)
            if (profit[i] < profit[position])
                position = i;

        return position;
    }

    //swaps values for city and profits
    public void swap(int i, int j)
    {
        // Swap the profits
        double temp = profit[i];
        profit[i] = profit[j];
        profit[j] = temp;

        // Swap the cities
        String str = city[i];
        city[i] = city[j];
        city[j] = str;
    }
}
4

1 回答 1

0

你没有提供你的测试/驱动程序,所以我不得不做出很多假设。

  1. 只需编写一个循环两个数组并打印值的方法。

  2. 您找到平均利润的方法看起来不错。

  3. 您有一种方法可以找到最高的利润,但您需要显示城市。我添加了一种方法来查找利润最高的城市。

    //shows city with highest profit
    String findHighestProfitCity() 
    {
        double highest = profit[0];
        String c = city[0];
    
        for(int i = 1; i < profit.length; i++)
        {
            if ( profit [i] > highest ) {
                highest = profit [i]; 
                c = city[i];
            }
        }
        return c;
    }
    
  4. 这是一个相当简单的方法。计算平均值后,只需遍历城市/利润数组并显示利润高于平均值的任何城市。

  5. 对组织在两个数组中的数据执行此操作的最简单方法是编写一个自定义排序例程,该例程对利润数组进行排序,并在每次需要利润时在城市数组中进行交换。

  6. 您放入城市和利润数组的测试数据太大而无法以图形方式显示。您的makeGraph()方法尝试为x利润中的每个值绘制星号。即使在每个值上去掉五个零之后,它仍然不适合我的屏幕,所以我修改了这个方法,只x/10为每个利润绘制星号

    for(int j = 1; j <= x/10; j++)
        s = s + "*";
    

这是一个测试/驱动程序,您可以将其用作起点。

package supermarkets;

public class SupermarketDriver {

/**
 * @param args
 */
public static void main(String[] args) {

    String[] cities = {"Miami", "Sunrise", "Hollywood",
            "Tallahassee", "Jacksonville", "Orlando", "Gainesville", "Pensacola",
            "Ocala", "Sebring"};

    double[] profits = {102, 146, 170, 60, 216, 91, 80, 125, 20, 45};


    Supermarkets sm = new Supermarkets(cities, profits);
    System.out.println(sm.makeGraph());
    System.out.println("Average profit: " + sm.average());
    System.out.println();
    System.out.println("Highest profit city: " + sm.findHighestProfitCity() + ", " + sm.findHighestProfit());

    }

}
于 2012-07-15T14:35:58.117 回答