我有六个任务需要完成,但我不知道如何显示它们。我拥有所有代码来计算所需的每个任务但是当它应用于我的测试器类时,我被卡住了。下面是我原来的类的代码。这些是需要完成的任务:
请帮助,因为我在过去几天一直这样做,但仍然无法做任何事情......我一定是傻请帮助
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;
}
}
答案 0 :(得分:0)
你没有提供你的测试/驱动程序,所以我不得不做出很多假设。
只需编写一个循环遍历两个数组并打印值的方法。
找到平均利润的方法看起来不错。
您有一种方法可以找到最高利润,但您需要显示城市。我添加了一种方法来找到利润最高的城市。
//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;
}
这是一种相当简单的方法。计算平均值后,只需遍历城市/利润数组,并显示任何利润高于平均值的城市。
使用两个数组组织的数据执行此操作的最简单方法是编写一个自定义排序例程,对利润数组进行排序,并在每次获利时在城市数组中进行交换。
您在城市和利润数组中放置的测试数据太大,无法以图形方式显示。您的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());
}
}