当调用ever函数-Java时,静态变量将再次初始化

当调用ever函数-Java时,静态变量将再次初始化,java,static,static-methods,Java,Static,Static Methods,我希望每次用户输入“y”以再次播放时都更新userScore和computerScore,但实际上,分数是从0初始化的。 我制作了一个ArrayList,以便在用户每次播放时显示结果。有人对如何更好地实现这一点有什么建议吗? 请忽略对DRY原则的违反:) import java.util.*; 导入java.io.*; 奥德桑登斯类{ 静态int UserScore=0; 静态积分=0; 静态ArrayList UserScoreArray=新ArrayList(); 静态ArrayList c

我希望每次用户输入“y”以再次播放时都更新userScore和computerScore,但实际上,分数是从0初始化的。 我制作了一个ArrayList,以便在用户每次播放时显示结果。有人对如何更好地实现这一点有什么建议吗? 请忽略对DRY原则的违反:)

import java.util.*;
导入java.io.*;
奥德桑登斯类{
静态int UserScore=0;
静态积分=0;
静态ArrayList UserScoreArray=新ArrayList();
静态ArrayList computerScoreArray=新ArrayList();
公共静态void main(字符串参数[]){
BufferedReader控制台=新的BufferedReader(新的InputStreamReader(System.in));
字符串名称=”,选项=”;
系统.out.println(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
System.out.println();
System.out.println(“让我们玩一个叫做赔率和均分的游戏”);
系统输出打印(“你叫什么名字?”);
试一试{
name=console.readLine();
}捕获(例外e){
System.out.println(“名称输入时出错:”+e);
}
系统输出打印(“Hi”+名称+”,您选择哪一种?(O)dds还是(E)vens?);
试一试{
choice=console.readLine().toLowerCase();
}捕获(例外e){
System.out.println(“选择奇数或偶数时出错:+e”);
}
if(选择。开始使用(“o”)){
System.out.println(name+“选择赔率,计算机将为偶数”);
}else if(choice.startsWith(“e”)){
System.out.println(name+“选择了EVENS,计算机将是赔率”);
}否则{
System.out.println(“输入有效选项“+name+”。O(O)或E(E)”);
}
系统.out.println(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
System.out.println();
while(播放(控制台、名称、选项、用户分数、计算机分数).startsWith(“y”)){
播放(控制台、名称、选项、用户分数、计算机分数);
}
}
公共静态字符串播放(BufferedReader控制台、字符串名称、字符串选择、int UserScore、int computerScore){
int numberOfFingers=0;
UserScoreArray.add(0);
computerScoreArray.add(0);
System.out.println(“你伸出多少个‘手指’(你只能伸出5个手指)”;
试一试{
numberOfFingers=Integer.parseInt(console.readLine());
}捕获(异常e1){
System.out.println(“取指数时出错:+e1”);
}
如果(手指数>5){
System.out.println(“不能伸出超过5个手指”+名称);
System.out.println(“让我们再试一次!”);
播放(控制台、名称、选项、用户分数、计算机分数);
}
Random rand=新的Random();
int computerFingers=rand.nextInt(6);
System.out.println(“计算机播放:“+computerFingers”);
int sum=手指数+计算机手指数;
System.out.println(“sum=”+numberOfFingers+“+”+computerFingers);
如果(总和%2==0){
System.out.println(sum+“是…偶数”);
if(选择。开始使用(“e”)){
System.out.println(“获胜者是”+name);
UserScore++;
添加(UserScore);
System.out.println(name+“)的分数:“+UserScoreArray.get(UserScoreArray.size()-1));
System.out.println(“计算机的分数:+computerScoreArray.get(computerScoreArray.size()-1));
}否则{
System.out.println(“获胜者是计算机”);
计算机评分++;
computerScoreArray.add(computerScore);
System.out.println(name+“)的分数:“+UserScoreArray.get(UserScoreArray.size()-1));
System.out.println(“计算机的分数:+computerScoreArray.get(computerScoreArray.size()-1));
}
}否则{
System.out.println(sum+“是…奇数”);
if(选择。开始使用(“o”)){
System.out.println(“获胜者是”+name);
UserScore++;
添加(UserScore);
System.out.println(name+“)的分数:“+UserScoreArray.get(UserScoreArray.size()-1));
System.out.println(“计算机的分数:+computerScoreArray.get(computerScoreArray.size()-1));
}否则{
System.out.println(“获胜者是计算机”);
计算机评分++;
computerScoreArray.add(computerScore);
System.out.println(name+“)的分数:“+UserScoreArray.get(UserScoreArray.size()-1));
System.out.println(“计算机的分数:+computerScoreArray.get(computerScoreArray.size()-1));
}
}
系统.out.println(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
System.out.println(name+“'的数组列表内容:“+”\n“+UserScoreArray);
系统.out.println(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
System.out.println(“计算机的数组列表内容:“+”\n“+computerScoreArray”);
系统.out.println(“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
System.out.println();
System.out.println(“乐谱看起来像“+name+”,想再玩一次吗?(y或n)”);
字符串“”再次播放;
试一试{
playreach=console.readLine().toLowerCase();
}捕获(例外e3){
System.out.println(“再次播放的功能错误:+e3”);
}
if(playreach.startsWith(“y”)){
System.out.println(“开始另一个游戏-”);
再回来玩;
}
否则{
System.out.println(“感谢您的参与,再见…”);
系统出口(0);
}
返回“p”;
}
}

试试这段代码,它会起作用的,你需要在
main
方法的开头初始化2个数组的
索引0

代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

class OddsAndEvens{
  static int UserScore = 0;
  static int computerScore = 0;
  static ArrayList<Integer> UserScoreArray = new ArrayList<Integer>();
  static ArrayList<Integer> computerScoreArray = new ArrayList<Integer>();

  public static void main(String args[]){
      UserScoreArray.add(0);
        computerScoreArray.add(0);

  BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String name = "", choice="";

    System.out.println("______________________________________________________________________________");
    System.out.println();
    System.out.println("Let's play a game called Odds and Evens");

    System.out.print("What is your name? ");
    try{
      name = console.readLine();
    } catch(Exception e){
      System.out.println("Error while name input: " + e);
    }

    System.out.print("Hi " + name + ", which do you choose? (O)dds or (E)vens? ");
    try{
      choice = console.readLine().toLowerCase();
    } catch(Exception e){
      System.out.println("Error while Choosing Odd or Even: " + e);
    }

    if(choice.startsWith("o")){
      System.out.println(name + " chose ODDS, the Computer will be EVENS");
    } else if(choice.startsWith("e")){
      System.out.println(name + " chose EVENS, the Computer will be ODDS");
    } else{
      System.out.println("Enter a valid choice "+ name + "..Either O(o) or E(e)");
    }

    System.out.println("______________________________________________________________________________");
    System.out.println();

    while(play(console, name, choice, UserScore, computerScore).startsWith("y")){
      play(console, name, choice, UserScore, computerScore);
    }

  }

  public static String play(BufferedReader console, String name, String choice, int UserScore, int computerScore){
    int numberOfFingers = 0;


    System.out.println("How many 'fingers' do you put out?(You can only put out 5 fingers)");
    try{
      numberOfFingers = Integer.parseInt(console.readLine());
    } catch(Exception e1){
      System.out.println("Error while, taking number of fingers: " + e1);
    }
    if(numberOfFingers > 5){
      System.out.println("You cannot put out more than 5 fingers " + name);
      System.out.println("Let's try again!");
      play(console, name, choice, UserScore, computerScore);
    }

    Random rand = new Random();
    int computerFingers = rand.nextInt(6);

    System.out.println("The computer played: " + computerFingers);

    int sum = numberOfFingers + computerFingers;
    System.out.println("sum = " + numberOfFingers  + "+" + computerFingers);

    if(sum%2 == 0){
      System.out.println(sum + " is. . . . Even");
      if(choice.startsWith("e")){
        System.out.println("The Winner is " + name);
        UserScore++;
        UserScoreArray.add(UserScore + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
      } else{
        System.out.println("The Winner is Computer");
        computerScore++;
        computerScoreArray.add(computerScore + computerScoreArray.get(computerScoreArray.size() - 1));
        System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
      }
    } else{
        System.out.println(sum + " is. . . . Odd");
        if(choice.startsWith("o")){
          System.out.println("The Winner is " + name);
          UserScore++;
          UserScoreArray.add(UserScore  + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
        } else{
          System.out.println("The Winner is Computer");
          computerScore++;
          computerScoreArray.add(computerScore + computerScoreArray.get(computerScoreArray.size() - 1));
          System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
        }
    }

    System.out.println("______________________________________________________________________________");
    System.out.println(name + "'s Array List contents: " + "\n" + UserScoreArray);
    System.out.println("______________________________________________________________________________");
    System.out.println("Computer's Array List contents: " + "\n" + computerScoreArray);
    System.out.println("______________________________________________________________________________");

    System.out.println();

    System.out.println("How does the Score look like " + name + ", Want to Play again?(y or n)");

    String playAgain = "";

    try{
      playAgain = console.readLine().toLowerCase();
    } catch(Exception e3){
      System.out.println("Error in function for playAgain: " + e3);
    }

    if(playAgain.startsWith("y")){
      System.out.println("Starting another game-");
      return playAgain;
    }
    else{
      System.out.println("Thank you for playing, see you soon..");
      System.exit(0);
    }
    return "p";
  }

}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.Random;
奥德桑登斯类{
静态int UserScore=0;
静态积分=0;
斯达
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

class OddsAndEvens{
  static int UserScore = 0;
  static int computerScore = 0;
  static ArrayList<Integer> UserScoreArray = new ArrayList<Integer>();
  static ArrayList<Integer> computerScoreArray = new ArrayList<Integer>();

  public static void main(String args[]){
      UserScoreArray.add(0);
        computerScoreArray.add(0);

  BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String name = "", choice="";

    System.out.println("______________________________________________________________________________");
    System.out.println();
    System.out.println("Let's play a game called Odds and Evens");

    System.out.print("What is your name? ");
    try{
      name = console.readLine();
    } catch(Exception e){
      System.out.println("Error while name input: " + e);
    }

    System.out.print("Hi " + name + ", which do you choose? (O)dds or (E)vens? ");
    try{
      choice = console.readLine().toLowerCase();
    } catch(Exception e){
      System.out.println("Error while Choosing Odd or Even: " + e);
    }

    if(choice.startsWith("o")){
      System.out.println(name + " chose ODDS, the Computer will be EVENS");
    } else if(choice.startsWith("e")){
      System.out.println(name + " chose EVENS, the Computer will be ODDS");
    } else{
      System.out.println("Enter a valid choice "+ name + "..Either O(o) or E(e)");
    }

    System.out.println("______________________________________________________________________________");
    System.out.println();

    while(play(console, name, choice, UserScore, computerScore).startsWith("y")){
      play(console, name, choice, UserScore, computerScore);
    }

  }

  public static String play(BufferedReader console, String name, String choice, int UserScore, int computerScore){
    int numberOfFingers = 0;


    System.out.println("How many 'fingers' do you put out?(You can only put out 5 fingers)");
    try{
      numberOfFingers = Integer.parseInt(console.readLine());
    } catch(Exception e1){
      System.out.println("Error while, taking number of fingers: " + e1);
    }
    if(numberOfFingers > 5){
      System.out.println("You cannot put out more than 5 fingers " + name);
      System.out.println("Let's try again!");
      play(console, name, choice, UserScore, computerScore);
    }

    Random rand = new Random();
    int computerFingers = rand.nextInt(6);

    System.out.println("The computer played: " + computerFingers);

    int sum = numberOfFingers + computerFingers;
    System.out.println("sum = " + numberOfFingers  + "+" + computerFingers);

    if(sum%2 == 0){
      System.out.println(sum + " is. . . . Even");
      if(choice.startsWith("e")){
        System.out.println("The Winner is " + name);
        UserScore++;
        UserScoreArray.add(UserScore + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
      } else{
        System.out.println("The Winner is Computer");
        computerScore++;
        computerScoreArray.add(computerScore + computerScoreArray.get(computerScoreArray.size() - 1));
        System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
        System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
      }
    } else{
        System.out.println(sum + " is. . . . Odd");
        if(choice.startsWith("o")){
          System.out.println("The Winner is " + name);
          UserScore++;
          UserScoreArray.add(UserScore  + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
        } else{
          System.out.println("The Winner is Computer");
          computerScore++;
          computerScoreArray.add(computerScore + computerScoreArray.get(computerScoreArray.size() - 1));
          System.out.println(name + "'s score: " + UserScoreArray.get(UserScoreArray.size() - 1));
          System.out.println("Computer's score: " + computerScoreArray.get(computerScoreArray.size() - 1));
        }
    }

    System.out.println("______________________________________________________________________________");
    System.out.println(name + "'s Array List contents: " + "\n" + UserScoreArray);
    System.out.println("______________________________________________________________________________");
    System.out.println("Computer's Array List contents: " + "\n" + computerScoreArray);
    System.out.println("______________________________________________________________________________");

    System.out.println();

    System.out.println("How does the Score look like " + name + ", Want to Play again?(y or n)");

    String playAgain = "";

    try{
      playAgain = console.readLine().toLowerCase();
    } catch(Exception e3){
      System.out.println("Error in function for playAgain: " + e3);
    }

    if(playAgain.startsWith("y")){
      System.out.println("Starting another game-");
      return playAgain;
    }
    else{
      System.out.println("Thank you for playing, see you soon..");
      System.exit(0);
    }
    return "p";
  }

}