gpt4 book ai didi

java - 如何使用增强的 for 循环打印数组

转载 作者:行者123 更新时间:2023-11-29 03:48:31 25 4
gpt4 key购买 nike

这是一个简单的程序,用于计算总销售额并确定有多少人在范围内赚取薪水(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);

}

最佳答案

double salesData[] = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};

for(double data : salesData){
gross = 200 + data * .09;
//your logic
}

关于java - 如何使用增强的 for 循环打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com