我是一名相当新的Java程序员,目前正在学习如何在一个代码中合并多个方法。此练习活动的目标是使用几种不同的方法:
创建两个阵列(一个用于员工姓名,另一个用于员工销售的数量)
- 找出总销售额的平均值
- 找出最高销售数量
- 找出销售额最高的员工的姓名(打印"万岁"对于销售额最高的每位员工)
import java.util.*;
public class MethodActivity{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] names={"Employee A", "Employee B", "Employee C", "Employee D", "Employee E", "Employee F", "Employee G", "Employee H", "Employee I", "Employee J"};
System.out.print("Enter the sales numbers, in dollars, for each employee: ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int num4 = sc.nextInt();
int num5 = sc.nextInt();
int num6 = sc.nextInt();
int num7 = sc.nextInt();
int num8 = sc.nextInt();
int num9 = sc.nextInt();
int num10 = sc.nextInt();
double[] sales={num1, num2, num3, num4, num5, num6, num7, num8, num9, num10};
return double[] sales;
return String[] names;
}
public static double getAverage(double[] sales){
double average=(num1+num2+num3+num4+num5+num6+num7+num8+num9+num10)/10;
return average;
}
public static int getHighestSale(double[] sales){
double highest = sales[0];
int locationOfHighest=0;
if(sales[1]>highest){
highest=sales[1];
locationOfHighest=1;
}else if(sales[2]>highest){
highest=sales[2];
locationOfHighest=2;
}else if(sales[3]>highest){
highest=sales[3];
locationOfHighest=3;
}else if(sales[4]>highest){
highest=sales[4];
locationOfHighest=4;
}else if(sales[5]>highest){
highest=sales[5];
locationOfHighest=5;
}else if(sales[6]>highest){
highest=sales[6];
locationOfHighest=6;
}else if(sales[7]>highest){
highest=sales[7];
locationOfHighest=7;
}else if(sales[8]>highest){
highest=sales[8];
locationOfHighest=8;
}else{
highest=sales[9];
locationOfHighest=9;
}
return highest;
}
public static String showName(String[] names){
String nameOfHighest = "";
String hooray = "";
for (int i = 0; i<names.length; i++){
if (i=locationOfHighest){
nameOfHighest=nameOfHighest+names[i]+", ";
hooray = ""+"hooray ";
}else{
nameOfHighest=nameOfHighest;
}
}
return nameOfHighest;
}
public static void (String[] args){
System.out.println("The average sales for today was: "+average);
System.out.println(nameOfHighest+" made the highest sales of "+highest);
System.out.println(hooray);
}
}
然而,当我运行程序时,我遇到了这些错误。问题是,我真的不明白他们的意思:
MethodActivity.java:20: error: '.class' expected
return double[] sales;
^
MethodActivity.java:21: error: '.class' expected
return String[] names;
^
MethodActivity.java:75: error: <identifier> expected
public static void (String[] args){
如果有人能澄清这些意思,我真的很感激,因为我仍然对多方法代码的概念感到困惑。并且,如果可以的话,也许可以在我的代码中指出任何其他问题或可修复的元素(因为我知道我的代码对于有编程经验的人来说看起来很邋and,我真的可以使用一些指针)。感谢您的时间。
答案 0 :(得分:1)
答案 1 :(得分:0)
而是使用单独的方法来打印“public static void(String [] args),在main方法本身中打印它们。 另请参阅iMBMT的回答。
答案 2 :(得分:0)
import java.util.Scanner;
public class MethodActivity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number of employees : ");
int totalEmployeeCount = sc.nextInt();
System.out.println("## totalEmployeeCount : " + totalEmployeeCount);
String[] employeeNames = new String[totalEmployeeCount];
int[] employeeSoldCount = new int[totalEmployeeCount];
String name;
int count;
for (int index = 0; index < totalEmployeeCount; index++) {
System.out.print("Enter employee name : ");
name = sc.next();
System.out.print("Enter employee sale count : ");
count = sc.nextInt();
employeeNames[index] = name;
employeeSoldCount[index] = count;
}
System.out.println("---------------- Pringting all info ----------------");
for (int i = 0; i < employeeNames.length; i++) {
System.out.println("name : " + employeeNames[i] + " & sale count : " + employeeSoldCount[i]);
}
findTheAverageOfTotalSales(employeeSoldCount);
findTheHighestSaleNumber(employeeSoldCount);
}
private static void findTheAverageOfTotalSales(int[] employeeSoldCount) {
for (int saleCount : employeeSoldCount) {
System.out.println("Write your own code ...");
}
}
private static void findTheHighestSaleNumber(int[] employeeSoldCount) {
for (int saleCount : employeeSoldCount) {
System.out.println("Write your own code ...");
}
}
}
答案 3 :(得分:0)
你需要学习更多,你的代码语法和逻辑有几个问题
试着
使用for loop
获取输入:
int nums=new int[10];
for(int i=0;i<10;i++){
nums[i]=sc.nextInt();
}
不要在void方法return
中使用(used in main method)
语句,除非您想要退出,但有更好的退出方式。
在return
声明中,您不需要指定返回类型(used at the end of main method)
,这是在方法定义中完成的:
正确的回报和方法定义:
public static String[] myMethod(){
String [] strings={"one","two"};
return strings;
}
寻找最高销售额使用循环:
public int indexOfHighestSale(double[] sales){
int highest=0;
int index=0;
for(int i=0;i<sales.length;i++){
if(sales[i]>highest){
highest=sales[i];
index=i;
}
}
return index;
}
您的上一个方法需要一个名称(标识符)和一些更正:
public static void print(String average,String nameOfBestSeller
,double highestSale,String hooray){
System.out.println("The average sales for today was: "+average);
System.out.println(nameOfHighest+" made the highest sales of "+highest);
System.out.println(hooray);
}
并且main方法应该按顺序调用方法并传递正确的参数:
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
String[] names={"Employee A", "Employee B", "Employee C", "Employee D", "Employee E", "Employee F", "Employee G", "Employee H", "Employee I", "Employee J"};
System.out.print("Enter the sales numbers, in dollars, for each
employee: ");
int [] sales =new int[10];
for(int i=0;i<10;i++){
sales[i]= sc.nextInt();
}
double average=getAverage(sales);
int index=indexOfHighestSale(sales);
print(average,names[index],sales[index],"hooray");
}