嗨,我一直在为经典的策划游戏编码。当没有重复的钉子时,该程序似乎完美地工作。
public static void guessingNumber(int [] digits){
print("Guess 5 digits numbers! ps.you only have 7 tries");
boolean guess = true;
int n = 0;
while(guess){ n++;
print("");
print("Guess no." + n + "!");
int [] yourGuess = manualNumber();
int correctPosition = 0;
int correctNumber = 0;
for(int i = 0; i<yourGuess.length; i++){
if(digits[i] == yourGuess[i]){
correctPosition += 1;
} else if(contains(digits, yourGuess[i]) && digits[i] != yourGuess[i]){
correctNumber += 1;
}
}
print("You have " + correctPosition + " correct place and " + correctNumber + " correct numbers.");
if(Arrays.equals(digits, yourGuess)){
guess = false;
print("");
print("Well Done! You did it!");
}
else if(n==10){
guess = false;
print("");
print("Game Over! You lose.");
}
}
}
public static void main(String[] args){
showMainMenu();
selectMode();
}
}
接下来我应该做什么才能允许重复钉?另外,我需要为cpu编写一个算法来猜测我的数字。欢迎提出建议!
答案 0 :(得分:0)
我对人类与CPU模式的认识(笑话:))
private static void runHumanVsCPU() {
int[] digits = manualNumber();
System.out.println("Guess no.1!\n"+digits[0]+digits[1]+digits[2]+digits[3]+digits[4]+"\nCPU have 5 correct place and 0 correct numbers.\n\nWell Done! CPU did it!");
}
P.S。为什么&#34; ps.you只有7次尝试&#34;但是
} else if (n == 10) {
guess = false;
print("");
print("Game Over! You lose.");
}