我遇到了一个循环问题,我的错误涉及&&

时间:2015-12-09 20:10:08

标签: java

我的方向是编写一个代码(在java中),找到1,000到9,999(任意4位数字)之间的代码,符合以下四个条件:

  1. 所有四位数都不同。
  2. 千位的数字是十位的数字的3倍。
  3. 这个数字很奇怪。
  4. 数字之和为27。
  5. 这是我到目前为止所拥有的。我的//loop部分下面出现错误:

    C4PP10.java:27: error: bad operand types for binary operator '&&'"
    

    是出现的错误。

    在我的脑海中,&&只是意味着“和”,我是不正确地使用它?

    这是我的java类家庭作业BTW的介绍。

    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
    

1 个答案:

答案 0 :(得分:0)

如此多的格式错误,请确保对布尔语句使用==而不是=进行比较,并使用缩进来提高可读性。 仅在逻辑语句中使用&&,而不是作为语句的补充。您还可以使用逗号(,)在开头定义多个变量,而不必每次都输入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!");
        }

    }
}