有人可以帮助我清除这些错误

时间:2016-04-06 19:54:15

标签: java data-structures

我正在尝试做一个总结谜题,问题要求通过枚举和测试所有可能的配置来使用求和谜题,然后它说用它来解决给出的例子。给出的例子是

锅+锅=围兜

狗+猫=猪

男孩+女孩=宝贝

我一直收到一个错误,指出作业的左侧必须是变量

  

charSet.charAt(setIndex ++)= stringTwo.charAt(loop);

无法从int转换为bool。

  

if(exists = 0)

同样在我尝试显示输出的代码中,它不会运行。

import java.util.Scanner;
public class Recursion
{
    // Example program


    public static String stringOne = new String(new char[10]);
    public static String stringTwo = new String(new char[10]);
    public static String stringThree = new String(new char[11]);
    public static String charSet = new String(new char[11]);
    public static int numberOne;
    public static int numberTwo;
    public static int numberThree;

    public static int maxCharCount;

    public static int[] numberSet = new int[10];

    public static void checkForEquality()
    {

    numberOne = numberTwo = numberThree = 0;
    int loop;
    int subloop;


        for (loop = 0; loop < stringOne.length(); loop++)

        {
            for (subloop = 0; subloop < maxCharCount; subloop++)

            {
            if (stringOne.charAt(loop) == charSet.charAt(subloop))

            {
            if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the number   

        numberOne = (numberOne * 10) + numberSet[subloop];
            }
            }

        }
        for (loop = 0; loop < stringOne.length(); loop++)

        {
            for (subloop = 0; subloop < stringTwo.length(); subloop++)

            {
            if (stringTwo.charAt(loop) == charSet.charAt(subloop))

            {
            if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the numeber  

        numberTwo = (numberTwo * 10) + numberSet[subloop];
            }
            }
        }


        for (loop = 0; loop < stringThree.length(); loop++)

        {
            for (subloop = 0; subloop < maxCharCount; subloop++)

            {
            if (stringThree.charAt(loop) == charSet.charAt(subloop))

            {
                if (loop == 0 && numberSet[subloop] == 0)

        return;

        //generate the number   

        numberThree = (numberThree * 10) + numberSet[subloop];
            }
            }
        }

        if (numberOne + numberTwo == numberThree)
        {
        //display the output 

        System.out.print("  Summation Puzzle solved.    ");

        System.out.print("\n");
        System.out.print(stringOne);
        System.out.print("<==>");
        System.out.print(numberOne);
        System.out.print("\n");
        System.out.print(stringTwo);
        System.out.print("<==>");
        System.out.print(numberTwo);
        System.out.print("\n");
        System.out.print(stringThree);
        System.out.print("<==>");
        System.out.print(numberThree);
        System.out.print("\n");

    //loop to show the result

    for (loop = 0; loop < maxCharCount; loop++)
    {
        System.out.print(charSet.charAt(loop));
        System.out.print("<==>");
        System.out.print(numberSet[loop]);
        System.out.print("\n");
    }

    System.exit(0);

        }
    }


        public static void generateCombinations(int indexCounter, int[] availableSet)

        {
            int loop;
            if (indexCounter != 0)
            {

        for (loop = 0; loop < 10; loop++)
        {

        numberSet[indexCounter] = loop;

        if (availableSet[loop] == 1)
        {

        availableSet[loop] = 0;

        generateCombinations(indexCounter + 1, availableSet);

        availableSet[loop] = 1;
        }
        }
            }

        if (indexCounter == maxCharCount)

        {
    checkForEquality();
        }

        }

        public static void createCharSet()
        {

        int loop;
        int setIndex; 
        int exists;
        int subloop;

        setIndex = 0;

        for (loop = 0; loop < stringOne.length(); loop++)
        {
            exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

        if (stringOne.charAt(loop) == charSet.charAt(subloop))
        {
        exists = 1;
        }

        }
        if (exists == 0)

        {
            charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));
        }
        }

            for (loop = 0; loop < stringTwo.length(); loop++)
            {
        exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

        if (stringTwo.charAt(loop) == charSet.charAt(subloop))

        {
        exists = 1;
        }

        }

        if (exists == 0)
        {

             charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringTwo.charAt(loop));

        }
            }

        for (loop = 0; loop < stringThree.length(); loop++)

        {
            exists = 0;

        for (subloop = 0; subloop < setIndex; subloop++)

        {

            if (stringThree.charAt(loop) == charSet.charAt(subloop))
            {
        exists = 1;
            }
        }

            if (exists == 0)
            {


            charSet = StringFunctions.changeCharacter(charSet, setIndex++, stringThree.charAt(loop));
            }
        }

            maxCharCount = setIndex;
        }

            public static void calculateSummation()

            {
            int loop;

        if (maxCharCount > 10)

        {

        System.out.print("Please check the input again");

        return;
        }
            else

            {

            int[] avaliableSet = new int[10];

        for (loop = 0; loop < 10; loop++)
        {

            avaliableSet[loop] = 1;
        }

            generateCombinations(0, avaliableSet);
            }
            }

     public static void main(String[]args)
            {
                Scanner scan = new Scanner(System.in);

            System.out.print(" Enter the first String :");
            stringOne = scan.next();


            System.out.print("  Enter the second String :");
            stringTwo = scan.next();

            System.out.print("  Enter the thirsd String :");
            stringThree = scan.next();

        createCharSet();



        System.out.print("  The character set formed from the given string = ");
        System.out.print(charSet);

        calculateSummation();
        checkForEquality();
            }
}

1 个答案:

答案 0 :(得分:1)

您的许多问题都源于您编写的代码中的语法错误。例如:

  1. line 74: if (stringThree.charAt(loop) == charSet.charAt(subloop) != null)

    • charSet.charAt(subloop) != null是无效的比较,因为!=运算符在与null比较时无法用于布尔值。如果您正在尝试确定是否存在从.charAt(var)返回的字符,请使用括号将每个object.charAt(var)的独立比较设为空。
  2. line 183: charSet = tangible.StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));

    • tangible具有讽刺意味的是没有实际意义,因为变量不存在于本地或尚未在全球范围内定义。
  3. charSet.charAt(setIndex++) = stringTwo.charAt(loop);

    • charSet.charAt(setIndex++)是一个返回字符的方法。这并不意味着您可以将字符设置为指定的索引,就像它是变量一样。
  4. line 227: if (exists = 0)

    • 在条件中进行比较时,您必须使用==
  5. line 269: Scanner scan = new Scanner(System.in);

    • 未导入Scanner类,因此无法使用。
  6. line 283: charSet.charAt(maxCharCount) = '\0';

    • 同样,您无法使用.charAt(var)设置该索引处的字符,就像它是变量一样。
  7. 所有这些问题都可以通过使用适当的IDE自行确定,例如Eclipse

    编辑:在编写代码来表示算法之前,尝试花一点时间用铅笔和纸来计算程序的逻辑。这样你就可以集中注意力,并且可以编写更全面,评论更清晰的代码。 Here是帮助缩小现有项目的指南。