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?
This is not how you compare Strings in java. It should be
You’re probably not hitting the code that would cause the score to be updated.
Also, change
To
As Dave Newton pointed out, you’d need to move these variables outside of the main function.