这是一个简单的程序,它计算总销售额并确定在范围内有多少人获得工资(200到299,300到399,等等......) 我已将值存储在数组中(计数器) 我如何使用增强的for循环来打印我的数组(不是计数器控制,只是增强)
这是我的程序
public class Sales
{
public static void main(String args[])
{
//declare
double salesData[] = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};
double gross = 0;//[] = new double[9];
int counter[]= new int[9];
int i=0;
//walk through sales data
for(i=0; i<salesData.length;i++)
{
//calculate gross
gross = 200 + salesData[i] * .09;
//determine range and increment count array
if((gross >200) && (gross <= 299))
counter[0] ++;
else if((gross >300) && (gross <= 399))
counter[1] ++;
else if((gross >400) && (gross <= 499))
counter[2] ++;
else if((gross >500) && (gross <= 599))
counter[3] ++;
else if((gross >600) && (gross <= 699))
counter[4] ++;
else if((gross >700) && (gross <= 799))
counter[5] ++;
else if((gross >800) && (gross <= 899))
counter[6] ++;
else if((gross >900) && (gross <= 999))
counter[7] ++;
else if (gross >1000)
counter[8]++;
//print gross
System.out.println(gross);
}
答案 0 :(得分:2)
double salesData[] = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};
for(double data : salesData){
gross = 200 + data * .09;
//your logic
}
答案 1 :(得分:1)
与在任何其他集合中使用它的方式相同:
for( double d : salesData) {
...
}
答案 2 :(得分:1)
Enhanced-for循环:
for(double b : salesData) System.out.println(b);
Arrays.toString(double[] array):
System.out.println(Arrays.toString(salesData));
答案 3 :(得分:0)
double[] salesData = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};
int[] counter = new int[9];
//walk through sales data
for (double salesDatum : salesData)
{
//calculate gross
double gross = 200 + salesDatum * .09;
int group = Math.min(8, (int)gross / 100 - 200);
counter[group]++;
//print gross
System.out.println(gross);
}