Java 如何在游戏中添加高分系统?
我正在尝试在双向飞碟射击游戏中实施高分制。这一切都在控制台中运行。我需要游戏用玩家的名字保存高分,所以我假设我需要将他们放入一个数组中。不过,我不知道如何在当前代码中将分数和名称添加到数组中Java 如何在游戏中添加高分系统?,java,Java,我正在尝试在双向飞碟射击游戏中实施高分制。这一切都在控制台中运行。我需要游戏用玩家的名字保存高分,所以我假设我需要将他们放入一个数组中。不过,我不知道如何在当前代码中将分数和名称添加到数组中 public class Target { public static int score = 0; public static int i = 0; public static String gameStart; public void TargetInfo(Player l) {
public class Target {
public static int score = 0;
public static int i = 0;
public static String gameStart;
public void TargetInfo(Player l) {
Scanner scan = new Scanner(System.in);
//Loop for all 25 targets
for(i=0; i<25; i++) {
System.out.println("\nhit Enter to shoot");
gameStart = scan.nextLine();
if(gameStart.equals("")){
int random1 = (int) (Math.random() * 105 + 15);
//Distance 15-35ft
if (random1 >= 15 && random1 <= 35) {
System.out.println("Your target is " + random1 + "ft away");
int random2 = (int) (Math.random() * 5 + 1);
if(random2<=3) {
score++;
System.out.println("You got one!");
}
else{
System.out.println("You missed");
}
}
//Distance 36-75ft
else if (random1 >=36 && random1 <=75) {
System.out.println("Your target is " + random1 + "ft away");
int random3 = (int) (Math.random() * 21 + 1);
if (random3 <= 11) {
score++;
System.out.println("You got one!");
}
else{
System.out.println("You missed");
}
}
//Target Distance 76-105ft
else if (random1 >=76 && random1 <=105){
System.out.println("Your target is " + random1 + "ft away");
int random4 = (int) (Math.random() * 11 + 1);
if (random4 <= 2) {
score++;
System.out.println("You got one!");
}
else{
System.out.println("You missed");
}
}
}
}
System.out.println(l.name + ", your score is: " + score);
}
公共类目标{
公共静态积分=0;
公共静态int i=0;
公共静态字符串gameStart;
公开无效目标信息(玩家l){
扫描仪扫描=新扫描仪(System.in);
//循环所有25个目标
对于(i=0;i=15&&random1,您可以通过多种方式执行此操作,例如添加一个整数数组和一个名称数组,或者一个哈希表。我个人更喜欢这种方式:
Hashtable<String, Integer> highScores = new Hashtable<String, Integer>();
Hashtable highScores=newhashtable();
通过这种方式,你可以将分数与特定玩家关联起来。这比为每个数组维护一个索引要容易得多
在每场比赛结束时,检查当前分数是否高于哈希表中的最低最高分数,然后可以更新它。将最高分数保存到文件中。