在更新我的问题之后,我知道每个团队只能获得0点的递归问题已被删除,以及如何解决此问题的任何想法。
在更新我的问题之后,我知道每个团队只能获得0点的递归问题已被删除,以及如何解决此问题的任何想法。
public static void main(String[] args) {
String homeside;
String awayside;
int homesidescore;
int awaysidescore;
homeside = "liverpool";
awayside = "Chelsea";
homesidescore = 0;
awaysidescore = 3;
int teamPoints = 0;
winnerIs(homeside, homesidescore, teamPoints, awayside, awaysidescore);
team1points(teamPoints);
team2points(teamPoints);
System.out.println(team1points(teamPoints));
System.out.println(team2points(teamPoints));
}
public static int winnerIs(String team1, int team1Score, int points, String team2, int team2Score){
int pointtoreturn=0;
if (team1Score > team2Score){
System.out.println("the winner is " + team1);
pointtoreturn= points +1;}
else if (team1Score == team2Score){
System.out.println("it is a draw");
pointtoreturn= points +3;}
else if (team1Score < team2Score){
System.out.println("the winner is " + team2);
pointtoreturn= points +2;}
return pointtoreturn;
}
public static int team1points(int points){
int p=0;
if (points == 1){
p= team1points(+3);
}
else if(points == 2){
p= team1points(+0);
}
else if(points == 3){
p= team1points(+1);
}
return p;}
public static int team2points(int points){
int p=0;
if (points == 1){
p= team2points(+0);
}
else if(points == 2){
p= team2points(+0);
}
else if(points == 3){
p= team2points(+3);
}
return p;}
//Any advice would be great
答案 0 :(得分:0)
teamPoints
永远不会在您的main
方法中获得更新。由于您的条件仅用于处理值1到3,这就是您返回-1的原因。根本问题似乎是winnerIs()
未被分配给任何内容的结果
答案 1 :(得分:0)
您的team1points和team2points都是故事递归的。你唯一的基本案例是
return -1;
所以,无论在递归调用中构造什么结果,它们都会消失,因为只要函数停止,你总是会返回-1。
答案 2 :(得分:0)
为什么使用递归调用?例如,return team1points(+3);
是递归调用,您应该return 3
。
此外,您未保存winnerIs
的结果,您应该使用teamPoints = winnerIs(...);
要从方法返回更多参数,您可以返回包含结果的类,例如
public class Points {
private int points1;
private int points2;
public Points(int points1, int points2) {
this.points1 = points1;
this.points2 = points2;
}
public int getPoints1() {
return points1;
}
public int getPoints2() {
return points2;
}
}
然后winnerIs
创建并返回此Points
对象。
在主要情况下,电话会是这样的:
Points points = winnerIs(homeside, homesidescore, teamPoints, awayside, awaysidescore);
System.out.println(points.getPoints1());
System.out.println(points.getPoints2());
答案 3 :(得分:0)
感谢所有帮助人员,这是我的最终版本,它很有效,我从这次练习中学到了很多东西,所有的指示都非常有益,对大卫很好。
public class Main {
public static void main(String[] args) {
String homeside;
String awayside;
int homesidescore;
int awaysidescore;
homeside = "liverpool";
awayside = "Chelsea";
homesidescore = 1;
awaysidescore = 1;
int teamPoints = winnerIs(homeside, homesidescore, awayside, awaysidescore);
team1points(teamPoints);
team2points(teamPoints);
System.out.println(team1points(teamPoints));
System.out.println(team2points(teamPoints));
}
public static int winnerIs(String team1, int team1Score, String team2, int team2Score) {
if (team1Score > team2Score) {
System.out.println("the winner is " + team1);
return 1;
} else if (team1Score == team2Score) {
System.out.println("it is a draw");
return 3;
} else if (team1Score < team2Score) {
System.out.println("the winner is " + team2);
return 2;
}
return 0;
}
public static int team1points(int points) {
if (points == 1) {
return +3;
} else if (points == 2) {
return +0;
} else if (points == 3) {
return +1;
}
return -1;
}
public static int team2points(int points) {
if (points == 1) {
return +0;
} else if (points == 2) {
return +3;
} else if (points == 3) {
return +1;
}
return -1;
}
答案 4 :(得分:-1)
试试这个:
public class Main {
public static void main(String[] args) {
String homeside;
String awayside;
int homesidescore;
int awaysidescore;
homeside = "liverpool";
awayside = "Chelsea";
homesidescore = 0;
awaysidescore = 3;
int teamPoints = 0;
winnerIs(homeside, homesidescore, teamPoints, awayside, awaysidescore);
team1points(teamPoints);
team2points(teamPoints);
System.out.println(team1points(teamPoints));
System.out.println(team2points(teamPoints));
}
public static int winnerIs(String team1, int team1Score, int points, String team2, int team2Score){
int pointtoreturn=0;
if (team1Score > team2Score){
System.out.println("the winner is " + team1);
pointtoreturn= points +1;}
else if (team1Score == team2Score){
System.out.println("it is a draw");
pointtoreturn= points +3;}
else if (team1Score < team2Score){
System.out.println("the winner is " + team2);
pointtoreturn= points +2;}
return pointtoreturn;
}
public static int team1points(int points){
int p=0;
if (points == 1){
p= team1points(+3);
}
else if(points == 2){
p= team1points(+0);
}
else if(points == 3){
p= team1points(+1);
}
return p;}
public static int team2points(int points){
int p=0;
if (points == 1){
p= team2points(+0);
}
else if(points == 2){
p= team2points(+0);
}
else if(points == 3){
p= team2points(+3);
}
return p;}
}