仅在数组中打印第一个值

时间:2015-02-12 10:23:08

标签: java arrays printing

无法打印数组中的所有值只能抓取它找到的第一个值并打印出来,它可以在最小值和最大值下正常工作,而不是低于avaergae名称o销售代表

import java.util.Scanner; 
import java.util.Arrays;
public class RepSales
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        String[]repName = new String[6];
        double[]salesAmount = new double[6];
        System.out.print("Please Enter The Month Which Sales Are To Be Recorded: ");
        String monthName = input.nextLine();
        System.out.println("\nPlease Enter Sales Reps Name Followed By Monthly Sales: \n");
        for (int i = 0; i < repName.length; i++ ) 
        {
            System.out.print("Sales Rep (Full Name):  "  );
            repName[i] = input.nextLine();
            System.out.print("Monthly Sales:  € "  );
            salesAmount[i] = input.nextDouble();
            input.nextLine();
            System.out.println();
        }
        Double max = salesAmount[0];
        Double min = salesAmount[0];
        Double valuesBelowAverage = salesAmount[0];
        double sum =0;
        double average =0;
        String maxName = repName[0];
        String minName = repName[0];
        String namesBelowAverage = repName[0];
        for (int i = 0; i < salesAmount.length; i++)
        {
            if (salesAmount[i] > max)
            {
                max = salesAmount[i];
                maxName = repName[i];
            }
            if(salesAmount[i] < min)
            {
                min = salesAmount[i];
                minName = repName[i];
            }
            sum = sum + salesAmount[i];
            average = sum/salesAmount.length;
            if (salesAmount[i] < average)
            {
                namesBelowAverage = repName[i] + ", ";//Only printing first value found
            }
        }
        System.out.println("\f");
        System.out.println("********************* ACME Sales Inc ************************");
        System.out.println("\n********************* Sales Report For " + monthName + " ************************");
        System.out.printf("List Of Sales Reps Below The Monthly Sales Average Of €" + "%.2f%n", average);
        System.out.println(namesBelowAverage);
        System.out.println("\nSales Rep With Highest Monthly Sales Of €" + max + " Was : " + maxName);
        System.out.println("\nSales Rep With Lowest Monthly Sales Of €" + min + " Was : " + minName);
    }
}

2 个答案:

答案 0 :(得分:1)

我认为你想要做的是(使用+=代替=):

namesBelowAverage += repName[i] + ", ";

但实际上,这不是打印数组,而是构建String(您应该考虑使用StringBuilder代替。)

答案 1 :(得分:0)

您需要两个循环 - 一个用于生成平均值,另一个用于测试销售人员。

   String namesBelowAverage = "";
   ...
   for (int i = 0; i < salesAmount.length; i++)
    {
        if (salesAmount[i] > max)
        {
            max = salesAmount[i];
            maxName = repName[i];
        }
        if(salesAmount[i] < min)
        {
            min = salesAmount[i];
            minName = repName[i];
        }
        sum = sum + salesAmount[i];
    }
    average = sum/salesAmount.length;
    for (int i = 0; i < salesAmount.length; i++)
    {
        if (salesAmount[i] < average)
        {
            namesBelowAverage += repName[i] + ", ";//Only printing first value found
        }
    }

还添加了Axel注意到的+ =。

编辑可将平均计算移出第二个循环。 修改修复了Axel的名称