基本上,这种掷骰子游戏几乎可以正常运行。在Java中运行它。我现在只有两个问题:
之前,我一直在将代码与许多掷骰游戏进行比较,但没有很多帮助,并且很少有示例可以说明问题。同样,我在代码中采用的方法主要由我的讲师强制执行,并尽我所能执行。如果我的代码中有任何令人发指的罪过,那是我的错,但需要很多复杂的东西。
import java.util.Scanner;
import java.util.Random;
//ISSUES: Roll 2 won't initiate a win if it equals the point. Win, loss, and game counters.
public class Craps_MCCH {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
System.out.println("Hey, over there. Yeah, you. What's your name?");
String name = keyboard.nextLine();
System.out.println("Are you serious? " + name + "? We don't use real names here.");
System.out.println("This is a serious operation. Give me your codename.");
String code = keyboard.nextLine();
System.out.println("Oh. " + code +". Okay. Sure.");
System.out.println();
System.out.println("So, want to play Craps?");
System.out.println("y/n");
char letter = '0';
letter = keyboard.nextLine().charAt(0);
while(letter == 'y' || letter == 'Y'){
System.out.println("Press Enter to roll.");
keyboard.nextLine();
int die1 = rand.nextInt(6)+1;
int die2 = rand.nextInt(6)+1;
System.out.println("For roll 1, you rolled a " + die1 + " and a " + die2);
int outcome = die1 + die2;
if(outcome == 7 || outcome ==11){
System.out.println("Wow, won already! No prize for new gize.");
}
else if(outcome == 2){
System.out.println("We call that snake-eyes. You lose. Do better");
}
else if(outcome == 12){
System.out.println("Choo-choo it's the boxcar children! Also you lose.");
}
else {
System.out.println(outcome + " is now your point. Roll it again to win.");
int die3 = rand.nextInt(6)+1;
int die4 = rand.nextInt(6)+1;
int outcome2 = (die3+die4);
System.out.println();
System.out.println("Let's roll. Press Enter.");
keyboard.nextLine();
int rcount = 3;
System.out.println("Roll 2: \n" + die1 + " and " + die2);
while(outcome2 != 7 || outcome2 != 11){
if(outcome == outcome2){
System.out.println("Huzzah! You've won!");
//win = win+1;
break;
}
else{
System.out.println("Point is " + outcome);
die3 = rand.nextInt(6)+1;
die4 = rand.nextInt(6)+1;
outcome2 = die3+die4;
System.out.println("Roll " + rcount++ + ":");
System.out.println("You rolled " + outcome2);
System.out.println("Press Enter.");
keyboard.nextLine();
}
}
if(outcome2 == 7 || outcome2 == 11){
System.out.println("After all that... no dice.");
//loss = loss+1;
}
}
//System.out.println("Current wins: " + win);
//System.out.println("Current losses: " + loss);
System.out.println();
System.out.println("Wins: ");
System.out.println("Losses: ");
System.out.println("Games played: ");
System.out.println();
System.out.println("Would you like to play again?");
System.out.println("y/n");
letter = keyboard.nextLine().charAt(0);
}
}
}
我希望比赛在第二局结束,但最终还是获胜,但我不知道该如何实现。此外,还需要计算我的赢,输和比赛。
我们鼓励我们尽可能地让我们的教授开心,因为他是一个愚蠢的家伙,不喜欢运行相同的无聊的旧程序,所以我尝试用他的打印语句来招待他,如果看起来很抱歉愚蠢的人,但只是希望它对他很有趣,并赋予它一些个性。
答案 0 :(得分:0)
要创建计数器,您需要使变量可用于整个方法。在键盘和兰特之后立即初始化游戏,赢和输计数器:
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int gameCount = 0;
int winCount = 0;
//int lossCount
您不需要损失计数。这只是gameCount-winCount。
与其说:
winCount = winCount + 1;
您可以说:
winCount++; //Which means the same thing, but looks professional
如果在循环中定义它们,则它们仅在该循环块中可用,并且每次循环重复时都会重置。
关于您的游戏为何未获得胜利的原因,仅使用break关键字 打破最内层的循环。这让您进入第一时间 循环。
此外,没有理由将字母分配给“ 0”。 只需声明一下即可:
char letter; //You don't need to assign it to anything.
答案 1 :(得分:-2)
我不知道如何玩掷骰子,但要重新考虑while循环。