我正在努力解决方法......尤其是你传递它的论点。

时间:2016-10-24 04:22:12

标签: java

我试图自学Java,而且我有一段时间了! :) 我前几天刚刚开始,我遇到了一些路障。

我试图学习数组,但我的方法也有点笨拙。

我知道当我在我的主要部门调用我的方法时,它是不对的。我以为我应该给我的方法,我正在调用它需要执行其进程的参数?

我只是试图调用我的上一个方法并显示该方法的数据。

package test_arraymethods;

import java.util.Scanner;

public class TEST_arraymethods 
{

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);

    System.out.println("------------------------------------------");
    System.out.println("Please enter the total number of dealers: ");
    System.out.println("------------------------------------------");
    int numDealers = input.nextInt();
    numDealers = numberOfDealers(numDealers);

    System.out.println("--------------------------------------------------------");
    System.out.printf("Please enter the required data for each of your dealers: %n");
    System.out.println("--------------------------------------------------------");
    dataCalculation(numDealers);

    //displayTotals(numberOfDealers, dealerNames, dealerSales, commissionRate);
}//main

//METHOD 1
public static int numberOfDealers(int dealers)
{
    int results;

    Scanner input = new Scanner(System.in);

    while(dealers < 0 || dealers > 30)
    {
        System.out.printf("%nEnter a valid number of dealers: %n");
        dealers = input.nextInt();  
    }
    results = dealers;
    return results;
}//number of dealers methods

//METHOD 2
public static void dataCalculation(int data)
{
    String[] dealerNames = new String[data];

    Scanner input = new Scanner(System.in);

    System.out.printf("%nEnter the names of the dealers:%n ");

        for(int i = 0; i < data; i++)
        {
            String names =input.nextLine();
            dealerNames[i]= names;
        }

    double[] dealerSales = new double[data];
    System.out.printf("%nEnter their sales totals: %n");

        for(int i = 0; i < data; i++)
        {
            double sales = input.nextDouble();
            dealerSales[i] = sales;
        }
}//data calculations

//METHOD 3
public static double[] commission(double[] dealerSales)
{
    //Create array
    double[] commissionRate = new double[dealerSales.length]; 

    for(int i = 0; i < dealerSales.length; i++)
    {
            commissionRate[i] = dealerSales[i];

        if(commissionRate[i] > 0 && commissionRate[i] < 5000)
           commissionRate[i] = commissionRate[i] * 0.08; 
        else if(commissionRate[i] > 5000 && commissionRate[i] < 15000)
            commissionRate[i] = commissionRate[i] * 0.15;
        else if(commissionRate[i] > 15000)
            commissionRate[i] =  commissionRate[i] * 0.20;
    }
    return commissionRate;
}//commission method 

public static double[] dealershipSales(double[] dealerSales)
{
    //Create array
    double[] dealershipSalesTotal = new double[dealerSales.length]; 

    for(int i = 0; i < dealerSales.length; i++)
    {
        dealershipSalesTotal[i] += dealerSales[i];

    }
    return dealershipSalesTotal;
}//dealership sales

public static double[] dealerSalesAvg(double[] dealerSales)
{
    double[] dealerSalesAvgTotal = new double[dealerSales.length];

    for(int i = 0; i < dealerSales.length; i++)
    {
        dealerSalesAvgTotal[i] += dealerSales[i] / dealerSales.length;
    }

    return dealerSalesAvgTotal;
}//dealership sales averages

public static double[] dealershipTotalCommission(double[] commissionRate)
{
    double[] totalCommission = new double[commissionRate.length];

    for(int i = 0; i < commissionRate.length; i++)
    {
            totalCommission[i] += commissionRate[i];
    }
    return totalCommission;
}//total commission for the dealership

public static void displayTotals(double[] numberOfDealers, double[] dealerNames, double[] dealerSales, double[] commissionRate)
{

    for(int i = 0; i < numberOfDealers.length; i++)
    {
        System.out.println(" " + dealerNames[i]);
        System.out.println(" " + dealerSales[i]);
        System.out.println(" " + commissionRate[i]);
    }


    // pass all of your values to this method and then display them
    //display the dealer name and amount of sales 
    //and the amount of commission for all dealers in a tabular format.

}//display totals */

}//class

1 个答案:

答案 0 :(得分:0)

您无法解决此问题的原因:

    displayTotals(numberOfDealers, dealerNames, dealerSales, commissionRate);

是指它不在范围内的变量。这些变量都是调用已完成的方法中的局部变量。如果您希望在另一个方法中访问变量,则必须将它们声明为(在本例中为静态)字段。