2016-10-17 31 views
0

我有我的编程课程的作业。我只有一个我无法弄清楚的问题。JAVA:骰子游戏,确保打印的是正确的

我的指示如下:编写一个程序,模拟玩家掷骰子100次后赢得的频率。如果玩家掷出一个7或11,他们赢了

我的问题如下:我的程序显示我比我应该赢得更多的时间。我只希望我的程序能够打印出用户赢得的消息,如果他们有七到十一个,但没有发生。任何人都可以就我可能需要做的事情给我一些建议吗?

以下是我的代码。非常感谢您的帮助。

@author Jordan Navas 
    @version 1.0 

    COP2253 Workshop7 
File Name: Craps.java 
*/ 

import java.util.Random; 

public class Craps 
{ 

public static void main(String[] args) 
{ 
    Random rand = new Random(); 
    int gamesWon=0; 
    int gamesLost=0; 

    for(int i=1;i<=100;i++) 
    { 
    craps(rand); 
    if(craps(rand)) 
    { 
       gamesWon++; 
    } 
    else{ 
      gamesLost++; 
    } 
    } 
    System.out.println("Games You Have Won: " + gamesWon); 
    System.out.println("Games You Have Lost: " + gamesLost); 

}  
public static boolean craps(Random rand) 
{ 
    int firstDice = rand.nextInt(6)+1; 
    int secondDice = rand.nextInt(6+1); 
    int sumOfDies = firstDice + secondDice; 
     System.out.print("[" + firstDice + "," + secondDice + "]"); 

     if (sumOfDies == 7 || sumOfDies == 11) 
     { 
      System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations! "); 
      return true; 
     } else if(sumOfDies == 2 || sumOfDies == 3 || sumOfDies == 12) 
     { 
      System.out.println(sumOfDies + " Congratulations! You Lost! "); 
      return false; 
     } 

    int point = sumOfDies; 
      System.out.print("Point: " + point + " "); 


    if (sumOfDies == point) 
    { 
     System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations!"); 
     return true; 
    } else 
     { 
     System.out.println(sumOfDies + " Congratulations! You Lost! "); 
     return false; 
     } 


     } 
    } 
+0

'如果(sumOfDies ==点)'总是返回TRUE; – piyushj

+0

不返回错误否定的是,虽然?这不够好吗?我应该删除那部分编码吗? –

+0

此代码将为'2,3或12'以外的任何其他内容返回'true' – piyushj

回答

0

我觉得你不需要下面的条件,如果你的代码。

if (sumOfDies == point) { System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations!"); return true; } 

事实上,所有你需要在你的代码是以下条件。

if (sumOfDies == 7 || sumOfDies == 11) { System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations! "); return true; } 
else { System.out.println(sumOfDies + " Congratulations! You Lost! "); return false; } 

这里是你的功能应该是如何工作的:

public static boolean craps(Random rand) { 
     int firstDice = rand.nextInt(6)+1; 
     int secondDice = rand.nextInt(6+1); 
     int sumOfDies = firstDice + secondDice; 
     System.out.print("[" + firstDice + "," + secondDice + "]"); 
     if (sumOfDies == 7 || sumOfDies == 11) { 
      System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations! "); 
      return true; 
     } else { 
      System.out.println(sumOfDies + " Congratulations! You Lost! "); 
      return false; 
    } 
} 
+0

我试过把它拿出来,但它弄乱了括号。 –