本质上,我想在我目前构建的猜谜游戏中输入一个评分系统,代码如下。目前,当用户猜测正确的数字时,他们会被告知他们的分数当前设置为 10。我想要做的是将分数设置为 10,并且每次猜测不正确时,该值都会减一。在用户玩游戏时,我也很难实现退出功能。你们能建议我如何将这些东西添加到我的代码中吗?注意:我在服务器上做猜谜游戏,我只会发布协议部分。
协议
import java.util.*;
public class KKProtocol {
int guess = 0, number = new Random().nextInt(100) + 1;
int score = 0;
Scanner scan = new Scanner(System.in);
public String processInput(String theInput) {
String theOutput = null;
System.out.println("Please guess the number between 1 and 100.");
while (guess != number) {
try {
if ((guess = Integer.parseInt(scan.nextLine())) != number) {
System.out.println(guess < number ? "Higher..." : "Lower...");
}
else {
System.out.println("Correct!");
score = 1;
}
}
catch (NumberFormatException e) {
System.out.println("Please enter a valid number! If you want to Quit just say'Goodbye'");
}
}
return theOutput;
}
}