2

I'm very new to programming and for some reason this program is not calculating the scores - my outprint when it comes to scores is consistently 0 (for example it just prints our score: 0 their score: 0) Here is my program:

public static void main (String[] args){
    Scanner in = new Scanner(System.in);
    boolean warmonger=false;
    int playerScore=0;
    int aiScore=0;
    theIntro();
    for (int i=0;i<GAMES;i++){
        scoreCounter(playerScore, aiScore);
        playerInput(in, warmonger, playerScore, aiScore);
        }
    }


public static void scoreCounter(int playerScore, int aiScore){
    System.out.println("=====");
    System.out.println("Our score: "+playerScore);
    System.out.println("Their score: "+aiScore);
    }

public static void playerInput(Scanner in, boolean warmonger, int playerScore, int aiScore){
    System.out.print("What is your strategy this year? ");
    String strat=in.next();
    if (strat.equalsIgnoreCase("peace")){
        peaceStrat(warmonger, playerScore, aiScore);
        }
    else if (strat.equalsIgnoreCase("war")){
        warStrat(warmonger, playerScore, aiScore);
        }
    else {
      while (!strat.equalsIgnoreCase("peace") && !strat.equalsIgnoreCase("war")){
            System.out.print("Invalid strategy.  Enter \"peace\" or \"war\": ");
            strat=in.next();
            }
        }
    }

public static void peaceStrat(boolean warmonger, int playerScore, int aiScore){
    String aiStrat=getStrategy("peace", warmonger);
    if (aiStrat=="peace"){          
        playerScore+=3;
        aiScore+=3;
        System.out.println("peace");
        }
    else if (aiStrat=="war"){
        aiScore+=5;
        System.out.println("we lost");
        }
}

public static void warStrat(boolean warmonger, int playerScore, int aiScore){
    warmonger=true;
    String aiStrat=getStrategy("war", warmonger);
    if (aiStrat=="peace"){
        playerScore+=5;
        System.out.println("we won");
        }
    else if (aiStrat=="war"){
        playerScore+=1;
        aiScore+=1;
        System.out.println("tie");
        }
}

Do I need to return the scores somehow?

Akaraka
  • 173
  • 1
  • 2
  • 13
  • 1
    Once you solve your problem, you will hit another one: `aiStrat=="war"`. This should be: `aiStrat.equals("war")`. See: http://stackoverflow.com/questions/513832 – Tomasz Nurkiewicz Oct 31 '11 at 19:42

2 Answers2

2

You're always modifying local versions of the score. Modifying an int in a method won't modify the int that was passed in to the method.

For now you could consider keeping the score as "class variables", i.e., static int variables, declared outside of the methods.

Eventually you'd probably want to wrap those up as instance variables; where they'd belong depends on how you end up breaking up application responsibilities.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
2
aiStrat=="peace"

This is not how you compare Strings in java. It should be

aiStrat.equals("peace");

You're probably not hitting the code that would cause the score to be updated.

Also, change

 int playerScore=0;
 int aiScore=0;

To

static int playerScore = 0;
static int aiScore = 0;

As Dave Newton pointed out, you'd need to move these variables outside of the main function.

zmf
  • 9,095
  • 2
  • 26
  • 28
  • 1
    +1 for the string comparison, I missed that. But you can't just change to `static int`, you need to move it outside of the `main` function. – Dave Newton Oct 31 '11 at 19:57