我为计算机科学课设计的猪骰子游戏不会在每个回合后保存每个单独的分数,即使玩家达到最高分数,我的游戏也不会停止(我知道boo lean是原因,但我不会不知道还有什么用)。同样,当玩家拒绝再次掷骰时,得分会回到零。如果有人帮助我,那就太好了!!谢谢你。
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;
}
}
答案 0 :(得分:0)
您的程序中存在逻辑缺陷。您正在使用得分= 0作为参数来调用滚动功能。
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);
}
在roll方法中,您要么返回0,要么每次返回的得分为0。因此,根据roll函数返回的值确定的p1score和p2score都永远永远为0。
这就是为什么游戏停留在while循环中而不会停止的原因。
您需要更改滚动功能以返回得分+您滚动的值。