有一道课后作业题,看起来很简单,可我是自学且以前没有基础,写得总不理想,特来求教:
实现Craps的游戏:(用java写)
Craps
玩家:1人
玩法:声明你希望赌多少钱,然后掷两粒骰子。如果点数之和是7或11,就赢得你下注的金额。如果点数之和是2、3或12,就会输掉你下注的钱。否则,点数之和就是你的得分。继续投掷骰子,直到你再次掷出了你的得分(在这种情况下你就赢了),或者你掷出了7(在这种情况下你输了)。
以下是我写的:
public class Die { private int topFace;
public Die(){
this.topFace=1; } public int getTopFace() {
return topFace;
} public void setTopFace(int topFace) {
this.topFace = topFace;
}
public void roll(){
this.topFace=((int)(Math.random()*6))+1;
}import java.util.Scanner;public class Crap { Scanner input = new Scanner(System.in); Die d1 = new Die(); Die d2 = new Die(); public Crap() {
} public void play() {
int sum, score;
System.out.println("you put money: ");
input.nextLine();
System.out.println("Hit to roll:"); d1.roll();
System.out.println("第一个的点数为: " + d1.getTopFace());
d2.roll();
System.out.println("第二个的点数为: " + d2.getTopFace());
sum = d1.getTopFace() + d2.getTopFace();
System.out.println("两个的点数之和为: " + sum); if ((sum == 7) || (sum == 11)) {
System.out.println("I win :" + input.nextLine());
} else if ((sum == 2) || (sum == 3) || (sum == 12)) {
System.out.println("I lose :" + input.nextLine());
} else {
score = sum;
System.out.println("score= " + score);
System.out.println("go on");
d1.roll();
System.out.println("第一个的点数为: " + d1.getTopFace());
d2.roll();
System.out.println("第二个的点数为: " + d2.getTopFace());
sum = d1.getTopFace() + d2.getTopFace();
System.out.println("两个的点数之和为: " + sum);
if (sum == score) {
System.out.println("I win :" + input.nextLine());
} else if (sum == 7) {
System.out.println("I lose :" + input.nextLine());
} else {
d1.roll();
d2.roll();
}
}
}
实现Craps的游戏:(用java写)
Craps
玩家:1人
玩法:声明你希望赌多少钱,然后掷两粒骰子。如果点数之和是7或11,就赢得你下注的金额。如果点数之和是2、3或12,就会输掉你下注的钱。否则,点数之和就是你的得分。继续投掷骰子,直到你再次掷出了你的得分(在这种情况下你就赢了),或者你掷出了7(在这种情况下你输了)。
以下是我写的:
public class Die { private int topFace;
public Die(){
this.topFace=1; } public int getTopFace() {
return topFace;
} public void setTopFace(int topFace) {
this.topFace = topFace;
}
public void roll(){
this.topFace=((int)(Math.random()*6))+1;
}import java.util.Scanner;public class Crap { Scanner input = new Scanner(System.in); Die d1 = new Die(); Die d2 = new Die(); public Crap() {
} public void play() {
int sum, score;
System.out.println("you put money: ");
input.nextLine();
System.out.println("Hit to roll:"); d1.roll();
System.out.println("第一个的点数为: " + d1.getTopFace());
d2.roll();
System.out.println("第二个的点数为: " + d2.getTopFace());
sum = d1.getTopFace() + d2.getTopFace();
System.out.println("两个的点数之和为: " + sum); if ((sum == 7) || (sum == 11)) {
System.out.println("I win :" + input.nextLine());
} else if ((sum == 2) || (sum == 3) || (sum == 12)) {
System.out.println("I lose :" + input.nextLine());
} else {
score = sum;
System.out.println("score= " + score);
System.out.println("go on");
d1.roll();
System.out.println("第一个的点数为: " + d1.getTopFace());
d2.roll();
System.out.println("第二个的点数为: " + d2.getTopFace());
sum = d1.getTopFace() + d2.getTopFace();
System.out.println("两个的点数之和为: " + sum);
if (sum == score) {
System.out.println("I win :" + input.nextLine());
} else if (sum == 7) {
System.out.println("I lose :" + input.nextLine());
} else {
d1.roll();
d2.roll();
}
}
}
解决方案 »
- 要通过一个事件关闭当前的对话框该用那个语句啊!
- 我要发泄啊,昨天用netbeans写了个java的gui的定时关机程序,结果...
- 依赖关系的几个小问题 各位大虾指导一下!
- 如何编写一个无人值守的程序,是不是JAVA不能写?懂的进来
- 关于 I/O 中 读取/写入时,使用 int型 的问题
- 为什么我的程序打包成jar后就不能访问ORACLE?
- 请看原程序?有错!我就是找不出来!在线等 !!!
- 如何使用这个jbuilder8的安装文件:nli_jb8.bin?
- 如何分解一个字符串为单个字符?(在线等待)
- 如何实现例如“下一步”这样的提示面板?
- java 如何开发出的程序给 c++/java /。net/delphi等等语言调用,如何作友好的接口?
- JAVA 初学者怎样才能JAVA高手?我想学WEB开发.能介绍一下应该学习哪些方面的东西,和推荐一些相关书籍吗?
賭輸的一注,廢物,廢話(vi.)擲骰子資料來源(2): Webster's Revised Unabridged Dictionary (1913) [web1913]
Crap \Crap\ (kr[a^]p), n.
In the game of craps, a first throw of the dice in which the
total is two, three, or twelve, in which case the caster
loses.
谢谢各位了