一个循环的做法。这个计划将会涉及到前一个。
对于这个程序,你将创建一个模拟骰子游戏猪。
该游戏的目标是让玩家在对手之前获得100分,其他细节将在任务中讨论。
在这种情况下,对手将是电脑。
任务1
这个游戏需要两个6面骰子。你需要用随机数发生器模拟这些骰子。
两个骰子(随机数生成器)必须:1-6 之间
农产品值具有140L和340L的种子,分别为模具和一个模2(用于测试目的) 任务2
现在你已经有了骰子,我们可以重复你将要玩的规则。
规则:
对于每一回合,玩家将轮流滚动两个骰子。 如果一个骰子没有出现在骰子上,这些值就会加到玩家的总数上。然后他们可以选择再次掷出(选择0)或将转牌传给其他玩家(选择1)。 如果某个骰子出现1,则该玩家在整个回合中获得的总积分不得增加,并成为其他玩家的回合(前一回合获得的积分将依然存在于其总数中)。 如果玩家掷出两个1s,则玩家的回合结束并且他们的总数被重置为0. 您需要一个随机数字生成器来确定计算机(0)或玩家(1)是否会先玩。这也将被用来模拟计算机的选择,以再次滚动或通过翻转。这台发电机的种子将是140L。
假定来自用户的有效输入。
输出应该
以下所有用户提示应该类似于语句“猪的博弈欢迎”开头:
轮到你了(目前积分:0) 您推出3和2,分获得本回合:5 按0再次或1卷启动电脑的回合 和所有计算机的提示应该是这样的:
电脑的回合(当前点:0) 电脑推出1和4,没有点赚取的轮到你 他们宣布哪个玩家的回合和当前分数。这是由什么播放器,然后
如果分荣获推出
然后是数字,显示赢得了该轮(看到用户的提示2号线) 如果一个被轧总积分,宣布获得无分,接下来的玩家回合(见电脑提示2号线) 如果同时的,利用显示消息“取其播放器/卷起两个1 /,点重置和/对手的/转”
import java.util.Scanner;
import java.util.Random;
public class GameOfPigs {
public static void main(String[] args) {
Random die1 = new Random(140L);
Random die2 = new Random(340L);
Random compDecision = new Random(140L);
Scanner scnr = new Scanner(System.in);
int computerTotal = 0;
int playerTotal = 0;
boolean playerTurn = true;
// Decides who goes first...
if ((compDecision.nextInt(2)) == 0) {
playersTurn = false;
}
System.out.println("Welcome of the Game of Pigs");
// Main game loop
while (computerTotal < 100 && playerTotal < 100) {
System.out.println();
int currentPlayerPoints = 0;
// Player's loop
while (playersTurn) {
System.out.println("Your turn (current points: " + playerTotal + ")");
int roll1 = die1.nextInt(6) + 1;
int roll2 = die2.nextInt(6) + 1;
// First Rule...Not the same as the example in class!!!
// Adjust accordingly!!!! Multiple ways to do this!!!!
if (roll1 == 1 && roll2 == 1) {
playerTotal = 0;
playersTurn = false;
break;
}
// Second Rule
else if (roll1 == 1 || roll2 == 1) {
playerTotal = playerTotal;
playersTurn = false;
break;
}
// Third Rule
else {
playerTotal += currentPlayerPoints;
int choice = scnr.nextInt();
if (choice == 1) {
playerTotal += currentPlayerPoints;
playersTurn = false;
break;
}
}
}
if (playerTotal >= 100) {
break;
}
//
int currentCompPoints = 0;
// Computer's loop
while (!playersTurn) {
System.out.println("Computer's turn (current points: " + computerTotal + ")");
int roll1 = die1.nextInt(6)+1;
int roll2 = die2.nextInt(6)+1;
if (roll1 == 1 && roll2 == 1) {
computerTotal = 0;
}
else if (roll1 == 1 || roll2 == 1) {
computerTotal = computerTotal;
}
else {
int choice = compDecision.nextInt(2);
computerTotal += currentPlayerPoints;
}
}
}
}
if (playerTotal > computerTotal) {
System.out.println("Congratulations! You won!");
}
else {
System.out.println("Too Bad, the computer won.");
}
}
}
GameOfPigs.java:86: error: illegal start of type
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: <identifier> expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: ';' expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: illegal start of type
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: <identifier> expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: ';' expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:87: error: illegal start of type
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: ';' expected
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: invalid method declaration; return type required
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: illegal start of type
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:89: error: class, interface, or enum expected
else {
^
GameOfPigs.java:91: error: class, interface, or enum expected
}
^
12 errors
谢谢!但现在它说:
GameOfPigs.java:18: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:29: error: cannot find symbol
while (playersTurn) {
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:29: error: illegal start of type
while (playersTurn) {
^
GameOfPigs.java:38: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:45: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:55: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:68: error: cannot find symbol
while (!playersTurn) {
^
symbol: variable playersTurn
location: class GameOfPigs
7 errors
“这个程序会比以前更复杂..”,请为我做作业吗?看起来你并没有付出太多努力,甚至试图自己先解决问题。即你在哪里开始和失败? –
现在我可以添加代码。对不起,以前,它不会让我。 – Muhammad
我确实试图首先自己解决问题。现在我能够发布我的代码,可以看到我开始和失败的位置。 – Muhammad