我對編程非常陌生,由於某種原因,這個程序並沒有計算出分數 - 當涉及到分數時,我的outprint始終爲0(例如,它只是打印我們的分數:0分數:0)這是我的程序:得分沒有被計算?
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");
}
}
我需要以某種方式返回分數嗎?
一旦你解決了你的問題,你會擊中另一個:'aiStrat ==「戰爭」'。這應該是:'aiStrat.equals(「戰爭」)'。請參閱:http://stackoverflow.com/questions/513832 –