我正在尝试创建一个迷你足球游戏,每次按ENTER时,它都会为每个球员提供一定的码数。我认为我遇到的问题是我想不出一种方法来使变量在每次转弯时都将码加在一起。当第一个函数初始化时,我得到了堆栈溢出递归错误。我也无法获取第一个函数进行初始化。我不知道那是否连接。
这是我到目前为止所拥有的
package Football;
import java.util.Scanner;
import java.util.Random;
public class FootballGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
int l = 0;
int m = 0;
Scanner in = new Scanner(System.in);
System.out.println(" THIS IS VIRTUAL FOOTBALL!");
System.out.println("In order to to take your turn and gain yards all you have to do is press ENTER");
System.out.println("First type your names");
System.out.println("player 1?");
String player1 = in.nextLine();
System.out.println("player 2?");
String player2 = in.nextLine();
System.out.println("OK! Lets play.");
in.next();
l = firstPlayer(player1, player2, l);
}
public static int firstPlayer(String player1, String player2, int l)
{
for(int i = 0; i >= 100; i++ )
{
Random r = new Random();
int rv = 0;
int y = r.nextInt(21);
l = (y + l);
System.out.println(player1 + " gained " + y + " yards for a total of " + l + " yards");
if(rv < 100)
{
secondPlayer(player1, player2, l);
}
if (l >= 100)
{
System.out.println("TOUCHDOWN!");
System.out.println("praise the lord almighty Jesus Jehova, God up above.");
break;
}
}
return l;
}
public static int secondPlayer(String player1, String player2, int m)
{
for(int i = 0; i >= 100; i++)
{
Random r = new Random();
int rv = 0;
int y = r.nextInt(21);
m = (y + m);
System.out.println(player2 + " gained " + y + " yards for a total of" + m + " yards");
if(m < 100)
{
firstPlayer(player1, player2, m);
}
if (m >= 100)
{
System.out.println("TOUCHDOWN!");
System.out.println(player2 + " cheated and won the game");
break;
}
}
return m;
}
}
任何帮助将不胜感激。