我在java上使用一些设定规则编写骰子游戏。
玩家寻求在骰子组合中获得7或11获胜。另一方面,如果他输了2,3或12。如果在第一次投掷期间你没有获得7或11(你赢了),或者获得2,3或12(你输了),游戏将进入第二阶段,你将在其中标记&# 34;点"在所述发射中获得的数量(4,5,6,8,9或10)。在这个阶段,射手将寻求在骰子中再次获得该数字,在获得7之前他将获胜。如果他设法重复点数,则玩家将获胜。另一方面,如果出现7,则会失败。
import myClass.dice1;
import myClass.dice2;
public class game{
public static void main(String [] args)
{
int t1, t2, total;
dice1 d1 = new dice1();
t1 = d1.tossdice();
dice2 d2 = new dice2();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1+t2;
if(total == 7 || total == 11)
{
System.out.println("Game won with "+ total);
}else if (total == 2 || total == 3 || total == 12)
{
System.out.println("Game lost with "+ total);
}else if (total == 4)
{
do
{
System.out.println("Total is "+total+" Throw again");
t1 = d1.tossdice();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1+t2;
break;
}while(total !=4 || total !=7);
if (total == 4)
{
System.out.println("Won game with "+ total);
}else
{
System.out.println("Lost game with "+ total);
}
}
}
}
我遇到的问题是,如果我开始了其他的话,那么如果我得到4,我必须再获得4才能赢,如果我得到7我输了,如果是另一个数字就扔直到4或7,问题是,当我得到一个4它"抛出"再一次,但如果除了4之外的任何东西都是一场失败的比赛,不知道这是否适用于我所拥有的突破线,但如果我不打破它,它会永远循环,我必须关闭cmd然后重新开始。
只有其他4,这是我所做的,因为一旦4的代码工作,我只需要对5,6,8,9和10做同样的事情。
我单独制作的这段代码
import myClass.dice1;
import myClass.dice2;
打印一下:
case 1: System.out.println(" ");
System.out.println(" o ");
System.out.println(" ");
根据t1和t2的值,它在案例1和6之间打印。
答案 0 :(得分:0)
以下代码修复了该问题。主要问题是:
以下是代码:
public class game {
public static void main(String[] args) {
int t1, t2, total;
dice1 d1 = new dice1();
t1 = d1.tossdice();
dice2 d2 = new dice2();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1 + t2;
if (total == 7 || total == 11) {
System.out.println("Game won with " + total);
} else if (total == 2 || total == 3 || total == 12) {
System.out.println("Game lost with " + total);
} else {
int winTarget = total;
System.out.println("Rolling until dice roll="+winTarget+" or 7");
do {
t1 = d1.tossdice();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1 + t2;
System.out.println("Total is " + total + " Throw again");
} while (total != winTarget && total != 7);
if (total == winTarget) {
System.out.println("Won game with " + total);
} else {
System.out.println("Lost game with " + total);
}
}
}