乘法求和解算器不起作用

问题描述 投票:-1回答:1

我正在上一堂课,我们应该求解看起来像这样的方程([] =数字):[][]*[][][]=[][][][]其中每个数字1-9只能使用一次。我编写的要解决的代码是[][]*[][][]=4396

我的代码没有错误,但是不会执行预期的操作

免责声明:该代码不会检查数字1-9是否仅使用一次,这是人类可以决定的(目前,请勿在任何示例代码中添加此功能)

这里是代码:

public class MK1 
{
public static void main(String[] args) 
{

    //the full sum
    long sum = 4396;


    long guess = 10000, guessCopy, otherSum = 0;
    short count = 0;

    //the digits used to guess the number
    long[] digits = new long[5];

    while(guess <= 99999)
    {

        //sets the different indexes of digits[] to the digits of guess
        guessCopy = guess;
        count = 0;
        while(guessCopy > 0)
        {

            digits[count] =  (guessCopy % 10);
            guessCopy = guessCopy / 10;

            count++;

        }            


        //determining if the guess is correct 
        otherSum = ((digits[4]*10) + digits[3]) * ((digits[2]*100) + (digits[1]*10) + digits[0]);

        if(otherSum == sum)
        {

//Print out digits that work
            for(int i = 0; i > digits.length; i++)
            {

                System.out.println(digits[i]);

//print out the separation between different solutions
                if(i == digits.length -1)
                {

                    System.out.println("next possible solution");

                }    

            }

        }

        //iterating the guess
        guess++;

    }
}
}
java sum
1个回答
0
投票

对于一个,您的数字在otherSum计算行中倒数。另外,循环打印数字的比较符号错误,应该为i < digits.length。这是您应该替换为的工作代码:

        //determining if the guess is correct 
        otherSum = ((digits[0]*10) + digits[1]) * ((digits[2]*100) + (digits[3]*10) + digits[4]);

        if(otherSum == sum)
        {

//Print out digits that work
            for(int i = 0; i < digits.length; i++)
            {

                System.out.println(digits[i]);

//print out the separation between different solutions
                if(i == digits.length -1)
                {

                    System.out.println("next possible solution");

                }    

            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.