我正在尝试将高分系统应用到我的双向飞碟射击游戏中。这一切都在控制台中运行。我需要游戏以玩家的名字保存高分,所以我假设我需要将它们放入阵列中。我不知道如何在我当前的代码中将分数和名称添加到数组中。
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 :(得分:0)
您可以通过多种方式执行此操作,例如添加一组int和一组名称,或Hashtable。就个人而言,我更喜欢这种方式:
Hashtable<String, Integer> highScores = new Hashtable<String, Integer>();
这样您就可以将分数与特定玩家联系起来。这比为每个数组维护索引更容易。
在每个游戏结束时,检查当前得分是否高于Hashtable中的最低得分,然后您可以更新它。 Here's the documentation.