
正文
关于21点java代码的信息
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java21点有没有好心人,可以在这段代码后面加一些注释啊。
包week0121点java代码;
进口java.awt.color;
进口java.awt.font;
进口java.awt.event.actionevent;
进口java.awt.event.actionlistener;
进口javax.swing.jbutton;
进口javax.swing.jframe;
进口javax.swing.jlabel;
进口javax.swing.joptionpane;
进口javax.swing.swingconstants;
进口javax.swing.border.lineborder;
公共课game21扩展JFrame {
私人JLabel label_2;
私有变量数;
私有变量和;
最后的JLabel标签=新jlabel();
最后label_1 =新jlabel() JLabel;
public static void main(String [] args){
新的game21();
}
公共game21() {
超(21点21点java代码?!”);
getcontentpane() setLayout(null);
这个setdefaultcloseoperation(JFrame。exit_on_close);
最后jbutton() JButton按钮=新;
按钮。addactionlistener(新actionlistener() {
public void actionPerformed(最终ActionEvent arg0){
onclick();
}
});
按钮设置(“出牌”);
按钮。窗口(17021点java代码,35021点java代码,106,28);
getcontentpane()添加(按钮);
标签。setborder(新lineborder(color.blue,1,假));
标签。sethorizontalalignment(swingconstants。中心);
标签。设置字体(新字体(“”,font.bold,26));
标签设置(“背面”);
标签窗口(158,81,137,153);
getcontentpane()添加(标签);
label_1。setText(“你已经拥有的牌:”);
label_1。窗口(109,22,270,45);
getcontentpane()添加(label_1);
这个窗口(200,300,501,528);
这个show(真的);
getcontentpane()添加(getlabel_2());
}
公共randnumber() { int
尝试{
线程。睡眠(10);
} catch(InterruptedException e){
E. printstacktrace();
}
返回(int)(数学。()* 10 + 1);
}
onclick() { public void
这randnumber()数量=;
this.sum + =数量;
标签设置(“+号);
字符串strtemp =这。label_1。gettext();
strtemp + =”+数字+”;
label_1 setText(strtemp);
字符串=“合计:”+笔;
label_2 setText(温度);
iswin();
}
iswin() { public void
如果(总和 21){
showmessagedialog JOptionPane。(这个,”你输了”);
clear();
返回;
}如果(和= 21)
showmessagedialog JOptionPane。(这个,”你赢了”);
clear();
返回;
其他{ }
int i = JOptionPane。showoptiondialog(这个,”是否继续?”、“提示”,
joptionpane.ok_cancel_option,
joptionpane.information_message,空,空,空的);
如果(我= = JOptionPane。ok_option){
onclick();
}其他
返回;
}
}
clear() { private void
label_2。setText(“合计:”);
总和= 0;
号码= 0;
label_1。setText(“你已经拥有的牌:”);
}
/ * *
*归
* /
保护带getlabel_2() {
如果(label_2 = = null){
label_2 =新jlabel();
label_2。setText(“合计:”);
label_2。窗口(313,35,66,18);
}
返回label_2;
}
}
直接翻译的。
相关问答
Q1: 急需一段用纯JAVA语言编的21点纸牌游戏的源代码
请不要发违规广告,是违法行为
请不要发违法消息,是违规的行为
请不要发违规广告,是违法行为
Q2: java,21点的题目
while(x=10)
{
System.out.print((int)x+" ");
System.out.println((double)Math.round(Math.sqrt(x)*100)/100);
x++;
}
(double)Math.round(Math.sqrt(x)*100)/100
这样就行了撒
Q3: 用java编程21点牌简单最好
public class Point21 {
private static ArrayListCardBean cardsOfCom;
private static ArrayListCardBean mycards;
private static int comStatus = 0;
private static int myStatus = 0;
private static int comMoney = 100;//电脑当前的钱
private static int playerMoney = 100;//游戏者当前的钱
private static int betScore = 1;//游戏者下注数
private static int fact = 1;//下注倍数
private static CardBean getOneCard(CardBean src) {
CardBean cardnew = new CardBean();
cardnew.setName(src.getName());
cardnew.setScore(src.getScore());
return cardnew;
}
private static int startGame(Cards52 c52) {
cardsOfCom.clear();
mycards.clear();
betScore = 1;
fact = 1;
CardBean cb = null;
CardBean newcard = null;
for (int i = 0; i 2; i++) {
cb = c52.getOneCard();
if (cb == null) {
return 0;
}
newcard = getOneCard(cb);
cardsOfCom.add(newcard);
cb = c52.getOneCard();
if (cb == null) {
return 0;
}
newcard = getOneCard(cb);
mycards.add(newcard);
}
return 1;
}
/**
* 显示电脑目前的牌和分数,如果返回值为0,则电脑“爆炸”了
* @return
*/
private static int showComputerCards() {
String cards = " ";
int total = 0;
for (int i = 0; i cardsOfCom.size(); i++) {
CardBean cb = (CardBean) cardsOfCom.get(i);
cards = cards + cb.getName() + " ";
total += cb.getScore();
}
System.out.println("Computer's cards:" + cards + ";Computer's score:"
+ total);
if(total==21)//当游戏双方有一方刚好是21点的时候,赌注的赔率翻倍..
{
fact = 2;
}
if (total 21) {
return 0;
}
return 1;
}
/**
* 显示游戏者目前的牌和分数,如果返回值为0,则游戏者“爆炸”了
* @return
*/
private static int showPlayerCards() {
String cards = " ";
int total = 0;
for (int i = 0; i mycards.size(); i++) {
CardBean cb = (CardBean) mycards.get(i);
cards = cards + cb.getName() + " ";
total += cb.getScore();
}
System.out.println("your cards:" + cards + ";your score:" + total);
if(total==21)//当游戏双方有一方刚好是21点的时候,赌注的赔率翻倍..
{
fact = 2;
}
if (total 21) {
return 0;
}
return 1;
}
/**
*
* @return 返回1则表明还有牌发,如果为0则表示最后一张牌发完,游戏结束!
*/
private static int computerGetOneCard(Cards52 c52) {
if (comStatus == 1)//如果这一局电脑已经不要牌了,则不能再次要牌
{
return 1;
}
int total = 0;
for (int i = 0; i cardsOfCom.size(); i++) {
CardBean cb = (CardBean) cardsOfCom.get(i);
total += cb.getScore();
}
if (total = 18) {
comStatus = 1;//超过17分电脑就不再要牌
return 2;
}
CardBean src = c52.getOneCard();
if (src == null)//牌已经全部发完,退出并返回0已告知主程序
{
return 0;
}
CardBean newcard = getOneCard(src);
cardsOfCom.add(newcard);
System.out.println("Computer get one card:" + newcard.getName());
return 1;
}
/**
*
* @return 返回1则表明还有牌发,如果为0则表示最后一张牌发完,游戏结束!
*/
private static int playerGetOneCard(Cards52 c52) {
if (myStatus == 1)//如果这一局游戏者已经不要牌了,则不能再次要牌
{
return 1;
}
System.out.println("您是否还要牌?是(y)/否(n)");
int ii = -1;
try {
ii = System.in.read();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
clearEnter();
char ch = Character.toChars(ii)[0];
if (ch == 'n') {
myStatus = 1;
return 2;
}
CardBean src = c52.getOneCard();
if (src == null)//牌已经全部发完,退出并返回0已告知主程序
{
return 0;
}
CardBean newcard = getOneCard(src);
mycards.add(newcard);
System.out.println("You get a card:" + newcard.getName());
return 1;
}
private static int compareScores() {
int pScore = 0;
int cScore = 0;
for (int i = 0; i mycards.size(); i++) {
CardBean cb = (CardBean) mycards.get(i);
pScore += cb.getScore();
}
for (int i = 0; i cardsOfCom.size(); i++) {
CardBean cb = (CardBean) cardsOfCom.get(i);
cScore += cb.getScore();
}
if (pScore cScore) {
return 1;
}
if (pScore cScore) {
return -1;
}
return 0;
}
private static void clearEnter() {
int ii = 13;
try {
while (ii == 13) {
ii = System.in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Point21() {
}
public static int play(Cards52 cards52) {
comStatus = 0;
myStatus = 0;
int ret = 0;
//如果把Cards52 放在这里来new的话。就是每次打完一局都会重新洗牌,程序永远不会结束
//Cards52 cards52 = new Cards52();
if (startGame(cards52) == 0) {
System.out.println("牌已发完,退出程序!");
return 1;
}
showComputerCards();
showPlayerCards();
doBetScore();//选择把下注的时机放在刚发两张牌的时候,这样比较刺激点^_^
int nofinish = 1;
int noabort = 1;
int finished = 0;
while (finished 2) {
noabort = computerGetOneCard(cards52);
if (noabort == 0) {
System.out.println("牌发完鸟,游戏结束!");
return 1;
} else if (noabort == 2) {
finished++;
if (finished == 2) {
break;
}
}
nofinish = showComputerCards();
if (nofinish == 0) {
System.out.println("电脑爆咯,你赢了!");
return 0;
}
noabort = playerGetOneCard(cards52);
if (noabort == 0) {
System.out.println("牌发完鸟,游戏结束!");
return 1;
} else if (noabort == 2) {
finished++;
}
nofinish = showPlayerCards();
if (nofinish == 0) {
System.out.println("你爆咯,你Failed!");
return 0;
}
}
if (finished == 2) {
int result = compareScores();
if (result == 1) {
System.out.println("你赢了!");
} else if (result == -1) {
System.out.println("你输了!");
} else {
System.out.println("打平!");
}
}
return ret;
}
private static void finishOneGame(int isfinish) {
try {
if(isfinish==0)//如果不是牌发完而导致游戏退出,则需算分...
{
calculateScores();
}
System.out.println("游戏结束!!!请按回车键继续");
System.in.read();
clearEnter();
//System.out.print("这里应该输入清屏...");
} catch (Exception e) {
System.out.println("error while game finished:" + e.getMessage());
}
}
private static void calculateScores()
{
//int nFact = 0;//决定游戏者,电脑是加分还是减分的因子
int pScore = 0;//游戏者的分数
int cScore = 0;//计算机的分数
int reward = 0;//最终得分
for (int i = 0; i mycards.size(); i++) {
CardBean cb = (CardBean) mycards.get(i);
pScore += cb.getScore();
}
for (int i = 0; i cardsOfCom.size(); i++) {
CardBean cb = (CardBean) cardsOfCom.get(i);
cScore += cb.getScore();
}
if(cScore21)//如果电脑爆了..
{
int s1 = cScore - 21;
reward = betScore * fact + s1;
}
else if(pScore21)//如果游戏者爆了
{
int s1 = pScore - 21;
reward = -(betScore * fact + s1);
}
else if(pScorecScore)//如果游戏者比电脑接近21点
{
reward = betScore * fact + (pScore - cScore);
}
else if(pScorecScore)//如果电脑比游戏者接近21点
{
reward = -(betScore * fact) + (pScore - cScore);
}
playerMoney += reward;
comMoney -= reward;
System.out.println("您现在的余额是:"+playerMoney+",电脑的余额:"+comMoney);
}
private static void doBetScore()
{
System.out.println("请输入 1~9 进行下注(如果输入其他字符,将默认为下注1)");
String str = readinOneString();
if("123456789".indexOf(str)=0)//正确的下注将按实际的下注值
{
betScore = Integer.parseInt(str);
}
else//非法的输入将认为是下1块钱注
{
betScore = 1;
}
}
private static String readinOneString()
{
String ret = null;
try
{
int ii = System.in.read();
clearEnter();
ret = "" + Character.toChars(ii)[0];
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
public static void main(String[] args) {
mycards = new ArrayListCardBean();
cardsOfCom = new ArrayListCardBean();
Cards52 cards52 = new Cards52(2);
int isfinish = 0;
while (isfinish == 0) {
isfinish = play(cards52);
finishOneGame(isfinish);
}
System.out.println("最后得分:游戏者"+playerMoney+";电脑"+comMoney);
}
}
public class CardBean {
private String name;
private String color;
private int score;
private int status;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
public class Cards52 {
public static final int ONE_PAIR_COUNT = 52;
//有多少副牌
private int size = 1;
private int unused = 0;
private ArrayListCardBean cards = null;
public Cards52()
{
initialize();
}
public Cards52(int nSize)
{
size = nSize;
initialize();
}
protected void initialize()
{
cards = new ArrayListCardBean();
for(int i=0;i4*size;i++)
{
for(int j=0;j13;j++)
{
initCardBean(j);
}
}
unused = ONE_PAIR_COUNT * size;
}
private void initCardBean(int index)
{
CardBean cd = new CardBean();
if(index==0)
{
cd.setName("A");
cd.setScore(11);
}
else if(index==10)
{
cd.setName("J");
cd.setScore(1);
}
else if(index==11)
{
cd.setName("Q");
cd.setScore(1);
}
else if(index==12)
{
cd.setName("K");
cd.setScore(1);
}
else
{
cd.setName(String.valueOf(index+1));
cd.setScore(index+1);
}
cd.setStatus(0);
cards.add(cd);
}
public CardBean getOneCard()
{
if(unused==0)
{
return null;
}
int seed = (int)System.currentTimeMillis() % 100;
Random rd = new Random();
int n = rd.nextInt(seed);
n = (Math.abs(n)) % cards.size();
CardBean cb = (CardBean)cards.get(n);
while(cb.getStatus()!=0)
{
n = (n+1) % cards.size();
cb = (CardBean)cards.get(n);
}
cb.setStatus(1);//这张牌已经发出去了,下次不能选了
unused--;
return cb;
}
}
21点java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、21点java代码的信息别忘了在本站进行查找喔。







