有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数学。随机if语句错误

    int Comproll1= (int) (Math.random()*6+1);
    int Comproll2= (int) (Math.random()*6+1);
      while (m==1)
    { 
        {
        if (Comproll1==1 || Comproll2==1)
        {
            System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!");
            cr= cr-cr;
            m++;
        }
        else if (Comproll1==1 && Comproll2==1)
        {
            System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!");
            cp=cp-cp;
            m++;
        }
        else 
        {
            cr= Comproll1+Comproll2;
            cp= cp+cr;
        }
    }

大家好!上面是我的代码——不管出于什么原因,它总是会,不管发生什么,总是显示第一个选项,即“计算机的掷骰子中有一个是1,它在这一轮中失去了所有的点数……”。即使我改变了语句的顺序,它仍然会这样做。有人能给我解释一下为什么会这样吗??谢谢


共 (3) 个答案

  1. # 1 楼答案

    问题是,您需要先检查它们是否都是1,然后再检查它们是否都是1。如果我们看一下代码:

    if (Comproll1==1 || Comproll2==1)
    {
        System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!");
        cr= cr-cr;
        m++;
    }
    else if (Comproll1==1 && Comproll2==1)
    {
        System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!");
        cp=cp-cp;
        m++;
    }
    

    如果:

    Comproll1=1

    Comproll2=1

    您希望它将进入else if (Comproll1==1 && Comproll2==1)但是,如果这是真的,那么if (Comproll1==1 || Comproll2==1)将始终是真的

    要解决此问题,只需更改if的顺序,如下所示:

    if (Comproll1==1 && Comproll2==1)
    {
        System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!");
        cp=cp-cp;
        m++;
    }
    else if (Comproll1==1 || Comproll2==1)
    {
        System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!");
        cr= cr-cr;
        m++;
    }
    

    希望这有帮助:)

    (你还需要重新掷骰子(正如艾略特·弗里希在回答中所说的那样))

  2. # 2 楼答案

    尝试更改if语句的顺序。从逻辑上讲,如果两个比较中的一个为真,则将执行第一条语句。在第二个条件else if (Comproll1==1 && Comproll2==1)为真的情况下,第一个条件if (Comproll1==1 || Comproll2==1)也将为真

    由于您已经以if-else-if方式链接了if语句,因此将执行第一个等同于true的if语句

        if (Comproll1==1 && Comproll2==1)
        {
            System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!");
            cp=cp-cp;
            m++;
        }
        else if (Comproll1==1 || Comproll2==1)
        {
            System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!");
            cr= cr-cr;
            m++;
        }
        else 
        {
            cr= Comproll1+Comproll2;
            cp= cp+cr;
        }
    
  3. # 3 楼答案

    据我所知,因为你没有滚动

    int Comproll1= (int) (Math.random()*6+1);
    int Comproll2= (int) (Math.random()*6+1);
    while (m==1)
    {
    

    应该是

    while (m==1)
    {
      int Comproll1= (int) (Math.random()*6+1);
      int Comproll2= (int) (Math.random()*6+1);
    

    此外,Java命名约定是变量的驼峰大小写(以小写字母开头)。所以,Comproll1可能是compRoll1。最后,我个人更喜欢^{}和6面骰子,可能看起来像

    Random rand = new Random();
    while (m==1)
    {
      int compRoll1 = rand.nextInt(6) + 1;
      int compRoll2 = rand.nextInt(6) + 1;
    

    编辑实际上,您还需要颠倒测试顺序。因为如果其中一个是真的,那么就永远不可能输入两个都是真的测试

    if (Comproll1==1 || Comproll2==1) {
      // Here.
    }else if (Comproll1==1 && Comproll2==1) {
      // Will never enter here.
    }
    

    将命令切换到

    if (Comproll1==1 && Comproll2==1) {
      // Both.
    }else if (Comproll1==1 || Comproll2==1) {
      // Either.
    }