在调用函数时,静态变量会再次初始化 - Java

时间:2018-03-17 15:32:49

标签: java static static-methods

我希望每次用户输入' y'时都会更新userScore和computerScore。再次玩,但相反,分数从0开始初始化。 我已经制作了一个ArrayList来显示每次用户播放时的结果。有没有人对此更好的实施有任何建议? 请忽略违反DRY委托人的行为:)

import java.util.*;
import java.io.*;

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[]){
    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;

    UserScoreArray.add(0);
    computerScoreArray.add(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);
        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);
        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);
          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);
          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";
  }

}

3 个答案:

答案 0 :(得分:0)

尝试使用此代码,它将起作用,您需要在index 0方法的开头

中初始化main个2个数组

以下是代码:

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";
  }

}

答案 1 :(得分:0)

  1. 请注意,在类的所有实例中都会看到静态变量。您不需要将其作为参数传递给方法。
  2. 如果您将其作为参数传递,请注意参数名称。如果您的变量被调用&#34;得分&#34;并且您的参数被称为&#34;得分&#34;同样,你正在改变什么(用&#34;得分++&#34;或其他&#34;得分&#34;操纵)不再是&#34;得分&#34;类变量,但是方法参数,退出方法时将不再显示对其执行的更改。这称为阴影。
  3. 我建议(对于类似的问题)一个SO的姐妹网站 - 一个处理代码审查的网站。 https://codereview.stackexchange.com/你可以在那里学到更多东西,因为在这里我们倾向于关注具体的问题。

答案 2 :(得分:0)

这是因为你的方法参数。你有两个不需要的 播放方法中的方法参数。只需删除UserScore&amp; 您的播放方法和代码中的computerScore个参数应运行良好。

在课堂上,UserScore&amp; computerScore字段是静态的。所以不行 需要将这些字段值传递给另一个静态方法 在您的代码中,这些参数只隐藏实际的静态 如果您在播放中打印UserScorecomputerScore的值,则会显示这些字段 方法,java只打印本地方法参数的值 而不是静态字段。