Java 将静态方法添加到类中,将数组运动员作为参数,并返回所有运动员赢得的奖牌总数nums
我已经上了一堂删除课,我只是不知道如何处理剩下的问题。我一直在尝试做一些根本不起作用的事情,但这就是我现在要做的。我还是一个初学者这是我学java的第一个学期,所以我可能不理解你们要补充的一些东西,所以请解释一下。 这就是他们要求我做的。 向类添加一个静态方法,该方法将运动员数组作为其参数,并返回数组中存储的所有运动员赢得的奖牌总数。试验方法Java 将静态方法添加到类中,将数组运动员作为参数,并返回所有运动员赢得的奖牌总数nums,java,arrays,loops,arguments,Java,Arrays,Loops,Arguments,我已经上了一堂删除课,我只是不知道如何处理剩下的问题。我一直在尝试做一些根本不起作用的事情,但这就是我现在要做的。我还是一个初学者这是我学java的第一个学期,所以我可能不理解你们要补充的一些东西,所以请解释一下。 这就是他们要求我做的。 向类添加一个静态方法,该方法将运动员数组作为其参数,并返回数组中存储的所有运动员赢得的奖牌总数。试验方法 package homework; import java.util.Arrays; public class Athlete { private S
package homework;
import java.util.Arrays;
public class Athlete {
private String name; // the name of the athlete
private String sport; // the sport the athlete does
private int numMedals; // the number of medals that the athlete has won
// constructor
public Athlete(String n, String s, int num) {
name = n;
sport = s;
numMedals = num;
}
// getters and setters for all instance variables
// (also called accessors and mutators)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSport() {
return sport;
}
public void setSport(String sport) {
this.sport = sport;
}
public int getNumMedals() {
return numMedals;
}
public void setNumMedals(int numMedals) {
this.numMedals = numMedals;
}
/* Returns a String with information about the athlete.
*/
public String toString() {
return name + " does " + sport + " and has won " + numMedals + " medal(s).";
}
public static void AthMedals(int[][]numMedals){
for(int i = 0; i < numMedals.length;i++){
int total = 0;
for(int j = 0; j < numMedals.length; i++);
total = numMedals.getNumMedals();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Athlete SA = new Athlete("Socrates","Baseball",5);
Athlete CC = new Athlete("Cesar","Baseball",3);
Athlete JA = new Athlete("Juan","Soccer",2);
System.out.println(SA);
System.out.println(CC);
System.out.println(JA);
System.out.println("SA has " +SA.getNumMedals()+ " medals.");
}
打包作业;
导入java.util.array;
公开课运动员{
私有字符串名称;//运动员的名称
私人弦乐运动;//运动员从事的运动
private int numMedals;//运动员获得的奖牌数量
//建造师
公共运动员(字符串n、字符串s、整数){
name=n;
运动=s;
nummdals=num;
}
//所有实例变量的getter和setter
//(也称为访问器和变异器)
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getSport(){
回归运动;
}
公共端口(字符串运动){
这个运动=运动;
}
public int getNumMedals(){
返回nummdals;
}
公共无效设置nummdals(整数nummdals){
this.numMedals=numMedals;
}
/*返回包含运动员信息的字符串。
*/
公共字符串toString(){
返回名称+“做”+运动+“并已获得”+numMedals+“奖牌”;
}
公共静态空间(int[][]nummdals){
对于(int i=0;i
}
}任务是编写如下方法
public static int sumOfAllMedals(Athlete[] all)
它返回all[0]。numMedals+all[1]。numMedals+…+所有[n]。numMedals
我假设ath
是您尝试这样做的,但实际上它只消耗处理能力,而不做任何事情
我不会帮你做家庭作业,但这里有个提示:
public static int sumOfAllMedals(Athlete[] all)
{
int total = 0;
// For every Athlete in `all`, add the number of medals (s)he's won
// Should be four lines at most ;)
return total;
}
静态方法必须迭代数组,并将每个奖牌添加到最终的返回变量中
static public int totalMedals(Athlete[] athelte) {
int totalMedals = 0;
for(int i=0;i<athelte.length;i++) {
totalMedals += athelte[i].numMedals;
}
return totalMeadls;
}
静态公共积分奖牌总数(运动员[]A){
整数=0;
对于(inti=0;iso),我必须添加:for(inti=0;iClass.StaticMethod(array)运动员。总奖牌(athelte);现在有人能帮我在我的主要方法中申报吗?