验证用户的输入Java

时间:2017-11-28 17:56:26

标签: java validation

Java新手,请帮忙。 需要验证才能接受负数。 创建2D阵列以显示Divisions和Quarters,用户提示输入数据 - 必须不是负数,然后每个Division / Quarter将显示数据,如果为负,则提示用户重新输入。 验证无效,我应该在哪里验证,我也可以在Main方法中调用它吗? 我的代码到目前为止......

//2D array to hold numbers of divisions and quarters
double [][] companyInfo; 


/**Method displays company info
@param company info.
*/

public static void enterSalesFigures(double [][] companyInfo)
{
    Scanner scanner = new Scanner(System.in); //Create a scanner object

    /*Validation for user's input
    while (companyInfo [divisionIndex][divisionIndex] < 0) 
    {
     System.out.println("Please enter the number that is more than or equal to one.");
     companyInfo [divisionIndex][divisionIndex] = scanner.nextDouble();
    }*/ 

        for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
        {
        for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
        {
            System.out.printf("Division %d, Quarter %d   ", divisionIndex + 1, quarterIndex + 1);
                    companyInfo [divisionIndex][divisionIndex] = scanner.nextDouble();
        }
        System.out.println();
    }
}


public static void listSalesFigures(double [][] companyInfo)
{
    double difference;//to hold difference value

    for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
    {
        difference = 0;

        System.out.printf("|Division %d|\n", divisionIndex +1);

        for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
            {
                if(quarterIndex >= 1)
                {
                    difference = companyInfo[divisionIndex][quarterIndex] - companyInfo[divisionIndex][quarterIndex - 1];
                }
            System.out.printf("|Quarter %d|: $%,.2f,\t\t |Difference To Previous Quarter|:( $%,.2f ).\n", 
                    quarterIndex + 1, companyInfo[divisionIndex][quarterIndex], difference);            
        }
    }
public static double [] calculateTotalQuarterlySales(double [][] companyInfo, int numberOfQuarters)
{
    double totalQuarterlySales; //total to accumulate sales for all quarters
    double[] totalQtSalesArray = new double[numberOfQuarters]; //array that will hold total for each quarter
    double difference = 0;
    System.out.println();

    for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
        {
        totalQuarterlySales = 0;

        for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
        {
            totalQuarterlySales += companyInfo[divisionIndex][quarterIndex];
        //totalQuarterlySales will increase by companyInfo
        }

        totalQtSalesArray [quarterIndex] = totalQuarterlySales;
        //check condition and calculate each division’s increase or decrease from 
        //the previous quarter starting with 
        if(quarterIndex >= 1)
        {
            difference = totalQtSalesArray[quarterIndex] - totalQtSalesArray[quarterIndex - 1];
        }

        System.out.printf("Total Sales Per Quarter |Quarter %d| $%,.2f,\t  |Difference To Previous Quarter|( $%.2f ).\n", 
                quarterIndex + 1, totalQtSalesArray[quarterIndex], difference);
    }
    //@return 
        return totalQtSalesArray;
        //reference variable that stores totalQtSalesArray array
}

//@param method To calculate the average sales for all divisions that quarter
public static void calculeQtSalesAverage(double [] totalQtSales, int numberOfDivisions) 
{
    System.out.println();
    for(int quarterlySalesIndex = 0; quarterlySalesIndex < totalQtSales.length; quarterlySalesIndex ++)
    {
        System.out.printf("Average Sales For \t|Quarter %d| $%,.2f.\n", quarterlySalesIndex + 1, 
                totalQtSales [quarterlySalesIndex] / numberOfDivisions);
    }
}

public static void showHighestSales(double [][] companyInfo)
{
    //hold highest sales value
    double highestSales;

    //hold highest sales value for a division
    int highestSalesDivisionIndex;

    for(int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++)
    {
        highestSales = 0;
        highestSalesDivisionIndex = 0;

        System.out.println();

        for(int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)
        {
            //check condition for any quarterly sales for any division to be 
            if(companyInfo[divisionIndex][quarterIndex] > highestSales)
            {
                highestSales = companyInfo[divisionIndex][quarterIndex];
                highestSalesDivisionIndex = divisionIndex + 1;
            }
        }
        //display division with the highest sales for that quarter
        System.out.printf("In Quarter %d Division %d had the highest sales in the amount of:\t $%,.2f.\n",
                quarterIndex + 1, highestSalesDivisionIndex + 1, highestSales);
    }
}

public static void main(String[] args) 
{
    int numberOfDivisions = 6; //hold number of divisions
    int numberOfQuarters = 4; //hold number of quarters

    double [] totalQtSales;

    double [][] companyInfo = new double [numberOfDivisions][numberOfQuarters];

    enterSalesFigures(companyInfo);
    listSalesFigures(companyInfo);
    totalQtSales = calculateTotalQuarterlySales(companyInfo, numberOfQuarters);
    calculeQtSalesAverage(totalQtSales, numberOfDivisions);
    showHighestSales(companyInfo);
}

}

1 个答案:

答案 0 :(得分:0)

    double[][] companyInfo = new double[10][10];

    Scanner scanner = new Scanner(System.in);

    for (int divisionIndex = 0; divisionIndex < companyInfo.length; divisionIndex++)

    {
        for (int quarterIndex = 0; quarterIndex < companyInfo[0].length; quarterIndex++) {
            System.out.printf("Division %d, Quarter %d   ", divisionIndex + 1, quarterIndex + 1);
            while ((companyInfo[divisionIndex][divisionIndex] = scanner.nextDouble()) < 0) {
                System.out.println("Please enter the number that is more than or equal to one.");
                System.out.printf("Division %d, Quarter %d   ", divisionIndex + 1, quarterIndex + 1);
            }

        }
    }

有帮助吗?