gpt4 book ai didi

java - 我在方法上苦苦挣扎……尤其是你通过它的论点。

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:31 25 4
gpt4 key购买 nike

我正在尝试自学 Java,但我玩得很开心! :)前几天我刚刚开始,但遇到了一些障碍。

我正在尝试学习数组,但我的方法也有点夸张。

我知道当我在 main 中调用我的方法时,它是不对的。我认为我应该给出我调用执行其过程所需的参数的方法?

我只是想调用我的最后一个方法并显示该方法中的数据。

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

最佳答案

无法使其工作的原因:

    displayTotals(numberOfDealers, dealerNames, dealerSales, commissionRate);

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

关于java - 我在方法上苦苦挣扎……尤其是你通过它的论点。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210648/

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