英文:
Create a recursive algorithm for multiplication with no parameters
问题
当计算等级时,相同类型的连续路径数量将具有越来越高的排名。例如,类型为“ice”的一个路径的排名为4。如果紧接着有另一个“ice”路径,第二个路径的排名将为8(2 * 4)。如果在该序列中有第三个“ice”路径,它的排名将为12(3 * 4)。坡道的总排名是其所有路径排名的总和,这个值必须赋给名为ranking的实例变量。
Trail是一个数组类型。所以,一个例子是[3,3,3,3],所以总排名应该是30。我正在尝试创建一个递归算法。
public int calculateRank() {
for (int i = 0; i < count; i++) {
if (trail[i].equals(0)) {
ranking = 0;
} else if (trail[i + 1] == trail[i]) {
ranking = calculateRank() * trail[i];
}
ranking += ranking;
}
return ranking;
}
Trail类
public class Trail {
private String ID;
private String type;
private int rank;
public Trail(String ID, String type) {
this.ID = ID;
this.type = type;
if (type.equals("ice")) {
rank = 4;
} else if (type.equals("trees")) {
rank = 3;
} else if (type.equals("rocks")) {
rank = 2;
} else if (type.equals("slalom")) {
rank = 1;
} else {
rank = 0;
}
}
public int getRank() {
return rank;
}
public void setRank(int newRank) {
this.rank = newRank;
}
public String getType() {
return type;
}
public void setType(String newType) {
this.type = newType;
}
public String getID() {
return ID;
}
public void setID(String newID) {
this.ID = newID;
}
public String toString() {
String s = "";
s += this.rank;
return s;
}
}
这是我写的代码,然而a.我并不完全理解我写的代码,b.它导致编译错误。
英文:
When calculating the rank, the number of consecutive Trails of the same type will have increasingly higher rankings. For example, one Trail of type "ice" has a ranking of 4. If there is another ice Trail immediately following it, that second one will have a ranking of 8 (2 *4). If there is a third ice Trail in that sequence, it will have a ranking of 12 (3 * 4).The overall rank of the slope is the sum of all its trails' rankings and this value must be assigned to the instance variable called ranking.
Trail is an array type. So, an example is [3, 3, 3, 3] so the overall rank should be 30. I am trying to create a recursive algorithm.
public int calculateRank () {
for (int i = 0; i < count; i++) {
if (trail[i].equals(0)) {
ranking = 0;
} else if (trail[i + 1] == trail[i]) {
ranking = calculateRank() * trail[i];
}
ranking += ranking;
}
return ranking;
}
Trail class
public class Trail {
private String ID;
private String type;
private int rank;
public Trail (String ID, String type) {
this.ID = ID;
this.type = type;
if (type == "ice") {
rank = 4;
} else if (type == "trees") {
rank = 3;
} else if (type == "rocks") {
rank = 2;
} else if (type == "slalom") {
rank = 1;
} else {
rank = 0;
}
}
public int getRank () {
return rank;
}
public void setRank (int newRank) {
this.rank = newRank;
}
public String getType () {
return type;
}
public void setType (String newType) {
this.type = newType;
}
public String getID () {
return ID;
}
public void setID (String newID) {
this.ID = newID;
}
public String toString () {
String s = "";
s += this.rank;
return s;
}
}
This is what I had written however a. I don't fully understand the code I wrote and b. It results in a compilation error.
专注分享java语言的经验与见解,让所有开发者获益!
评论