Java I';我的循环有问题,我的错误涉及&&;

Java I';我的循环有问题,我的错误涉及&&;,java,Java,我的指导是编写一个代码(用java)来查找1000到9999(任意4位数字)之间的代码,该代码满足以下四个条件: 所有四个数字都不同 千位数字是十位数字的3倍 这个数字是奇数 数字之和是27 这就是我目前所拥有的。我在我的//循环部分下得到一个错误: C4PP10.java:27: error: bad operand types for binary operator '&&'" 是出现的错误 在我的脑海中,&&的意思就是“和”,我是否使用不当 顺便说一句,这是我的java类

我的指导是编写一个代码(用java)来查找1000到9999(任意4位数字)之间的代码,该代码满足以下四个条件:

  • 所有四个数字都不同
  • 千位数字是十位数字的3倍
  • 这个数字是奇数
  • 数字之和是27
  • 这就是我目前所拥有的。我在我的
    //循环
    部分下得到一个错误:

    C4PP10.java:27: error: bad operand types for binary operator '&&'"
    
    是出现的错误

    在我的脑海中,
    &&
    的意思就是“和”,我是否使用不当

    顺便说一句,这是我的java类家庭作业介绍

    public class C4PP10
    {
        public static void main(String[] args)
        {
            //variables
            int first=1;
            int second=0;
            int third=0;
            int fourth=0;
            int total = first * 1000 + second * 100 + third * 10 + fourth;
            boolean fourDifferentDigits = false;
            boolean thousandsPlaceX3TensPlace = false;
            boolean odd = false;
            boolean sum = false;
            boolean correctAddress = false;
    
            //loop
            while (correctAddress = false)
            {
                fourth = fourth + 1;
                if (fourth == 10)
                {
                    fourth = 0 && third = third + 1;
                }//end if
                if (third == 10)
                {
                    third = 0 && second = second + 1;
                }//end if
                if (second == 10)
                {
                    second = 0 && first = first + 1;
                    total = first * 1000 + second * 100 + third * 10 + fourth;
                }//end if
            }//end loop
    
    
            //testing
            if ( first != second && first !=third && first !=fourth && second !=third &&                 second !=fourth && third != fourth )
            {
                fourDifferentDigits = true;
            }//end if
    
            if (first == third*3)
            {
                thousandsPlaceX3TensPlace = true;
            }//end if 
    
            if (fourth%2 != 0)
            {
                odd = true;
            }//end if
    
            if ( first + second + third + fourth == 27 )
            {
                sum = true;
            }//end if
    
            if (fourDifferentDigits=true && thousandsPlaceX3TensPlace=true && odd=true   && sum=true)
            {
                correctAddress = true;
            }//end if
    
            if (correctAddress = true)
            {
                System.out.println("The Riddler plans to strike " + total + " Pennsylvania     Avenue!");
            }
    
        }//end main
    }//end class
    

    如此多的格式错误,请确保在比较时,对布尔语句使用
    =
    而不是
    =
    ,并使用缩进以提高可读性。 仅在逻辑语句中使用
    &&
    ,而不是作为语句的补充。您也可以在开始时使用逗号(
    )定义多个变量,而不必每次都键入
    int

    public class C4PP10
    {
        public static void main(String[] args)
        {
            //variables
            int first = 1, second = 0, third = 0, fourth = 0;
            int total = first * 1000 + second * 100 + third * 10 + fourth;
            boolean fourDifferentDigits = false, thousandsPlaceX3TensPlace = false;
            boolean odd = false, sum = false, correctAddress = false;
    
            while (correctAddress == false) // or while (!(correctAddress))
            {
                fourth = fourth + 1;
                if (fourth == 10)
                {
                    fourth = 0;
                    third = third + 1;
                }                
                if (third == 10)
                {
                    third = 0
                    second = second + 1;
                }
                if (second == 10)
                {
                    second = 0 
                    first = first + 1;
                    total = first * 1000 + second * 100 + third * 10 + fourth;
                }
            }
    
    
            if (first != second && first != third && first != fourth && second !=third && second !=fourth && third != fourth)
            {
                fourDifferentDigits = true;
            }
    
            if (first == third * 3)
            {
                thousandsPlaceX3TensPlace = true;
            }
    
            if (fourth % 2 != 0)
            {
                odd = true;
            }
    
            if (first + second + third + fourth == 27)
            {
                sum = true;
            }
    
            if (fourDifferentDigits == true && thousandsPlaceX3TensPlace == true && odd == true && sum == true)
            {
                correctAddress = true;
            }
    
            if (correctAddress == true)
            {
                System.out.println("The Riddler plans to strike " + total + " Pennsylvania Avenue!");
            }
    
        }
    }
    

    &&
    是一个布尔运算符,不是拼写英语连词“and”的有趣方式。它比英语连词的作用小得多。是的。你想要一个
    在while(correctAddress=false)
    中有
    &
    while(correctAddress=false)
    while(!correctAddress)
    while(correctAddress!=true)
    。一个等于赋值。在
    循环中,您只使用了一个
    =
    ,因此它将
    false
    赋值给
    correctAddress
    变量,从而结束循环,因为赋值是
    false
    。如果你想要的是一个比较,那么你必须使用
    ==
    @RobertoLinares。然而,当他比较布尔值时,他将其写成
    while(correctAddress){
    并使用
    =
    =
    防止打字错误,以免将OP与另一个运算符混淆,但倒数第二个if语句可以替换为
    correctAddress=fourDifferentidDigits&thousandsPlaceX3TensPlace&odd&sum
    。实际上,所有if语句都可以删除,并将变量分配给ch埃克斯。