我的电脑科学类的猪粪骰子游戏不会在每次回合后保存每个人的分数,我的游戏甚至在玩家达到最高分数后也不会停止(我知道boo斜度是原因,但我不知道还可以使用什么)。同样,当一名球员再次拒绝翻滚时,比分会回到零。如果有人帮我,那就太好了!!谢谢你。
import java.util.*;
import java.util.Random;
public class PigDiceGamebyJian {
public static Scanner sc = new Scanner(System.in);
public static Random gen = new Random();
public static void main(String[] args) {
//char repeat;
System.out.println(" Welcome to the Pig Dice Game ");
System.out.println("This game requires two players");
System.out.println("How to play: each player will take turn rolling the dice (adding up the turns) until the sum is 30");
System.out.println("First one to get to 30 wins, though if on a turn, if you roll a 1, ");
System.out.println("you will give the dice to the other player, and you will not add anything to your score because 1 = 0");
System.out.println("Enough with the boring rules.");
String p1 = getName();
String p2 = getName();
System.out.println("Hello " + p1 + " and " + p2 + ".");
System.out.println("Please enter a score that you would like to play up to");
final int fin = sc.nextInt();
System.out.println(p1 + " will be going first.");
int p1score = roll(p1, 0, fin);
int p2score = roll(p2, 0, fin);
while (p1score < fin && p2score < fin ) {
p1score += roll(p1, 0, fin);
p2score += roll(p2, 0, fin);
}
}
private static int roll(String player, int score, int fin) {
boolean go = true;
int counter = 0;
while(go) {
int dice = gen.nextInt(6) + 1;
if (dice == 1) {
System.out.println(player + " You rolled 1, your turn is over.");
System.out.println(player + " your total is " + counter);
return 0;
}
System.out.println(player + " you rolled a " + dice);
counter = counter + dice;
System.out.println(player + " Your turn score is " + counter + " Do you want to roll again? (y)es (n)o");
String ans = sc.next();
if (ans.equals("n")) {
go = false;
System.out.println(player + " your score is " + score);
return score;
}
if (score > fin) {
go = false;
System.out.println(player + " you've won the PIG DICE GAME!!");
}
}
return score;
}
private static String getName() {
System.out.println("Enter the name for a player.");
String player = sc.next();
return player;
}
}发布于 2018-12-10 17:42:03
您的程序中有一个逻辑缺陷。您正在以score =0作为参数调用roll函数。
int p1score = roll(p1, 0, fin);
int p2score = roll(p2, 0, fin);
while (p1score < fin && p2score < fin ) {
p1score += roll(p1, 0, fin);
p2score += roll(p2, 0, fin);
}在滚动方法中,要么返回0,要么每次返回分数为0。因此,根据roll函数返回的内容确定的p1score和p2score永远都是0。
这就是为什么你的游戏不停止,因为它被困在时间循环。
您需要更改roll函数以返回分数+已滚动的值。
https://stackoverflow.com/questions/53710725
复制相似问题