本文介绍了有人能帮我清除这些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



pot + pan = bib



dog + cat = pig



男孩+女孩=宝贝



我不断收到一个错误,说左派分配必须是一个变量

无法从int转换为bool。

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

  import java.util.Scanner; 
public class Recursion
{
//示例程序


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;

//生成数字

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

}
(循环= 0;循环< stringOne.length();循环++)

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

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

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

return;

//生成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;

//生成数字

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

if(numberOne + numberTwo == numberThree)
{
//显示输出

System.out.print(Summation Puzzle solve);

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

//循环显示结果

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

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;
}
}
}

如果(indexCounter == maxCharCount)

{
checkForEquality();
}



public static void createCharSet()
{

int loop;
int setIndex;
int存在;
int subloop;

setIndex = 0; ($循环= 0;循环< stringOne.length();循环++)
{
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(循环));



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(请重新输入输入);

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)
{
扫描仪扫描=新的扫描仪(System.in);

System.out.print(输入第一个String:);
stringOne = scan.next();


System.out.print(输入第二个String:);
stringTwo = scan.next();

System.out.print(输入thirsd String:);
stringThree = scan.next();

createCharSet();



System.out.print(由给定的字符串组成的字符集=);
System.out.print(charSet);

calculateSummation();
checkForEquality();
}
}


解决方案

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


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




    • charSet.charAt(subloop)!= null 是一个无效的比较,因为与 null != 运算符不能用于布尔值c>。如果要确定字符是否从 .charAt(var)返回,请使用括号对每个 object.charAt进行独立比较( var)为null。


  2. 第183行:charSet = .StringFunctions.changeCharacter(charSet,setIndex ++,stringOne.charAt(loop));




    • 有形的具有讽刺意义,因为该变量在本地不存在或尚未在全球定义。


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




    • charSet.charAt(setIndex ++)是一种返回字符的方法。这并不意味着您可以设置指定索引的字符,就像它是一个变量。


  4. 行227:if(exists = 0)




    • 您必须使用 == 在进行条件比较时。


  5. 行269:扫描仪扫描= new Scanner(System.in);




    • Scanner 类未导入,因此无法使用。


  6. 第283行:charSet。 charAt(maxCharCount)='\0';




    • 再次,您不能使用 .charAt(var)设置该索引处的字符,就像它是一个变量。




所有这些问题都可以通过使用适当的IDE进行自我确定,例如。



编辑:尝试花点心在编写代码以表示您的算法之前,用铅笔和纸张更多时间处理程序的逻辑。这样你就有了焦点,可以编写更全面,更有用的,更清晰的代码。 有一点的帮助缩小您现有项目的指南。


I am trying to do a summation puzzle, the questions asks to use summation puzzles by enumerating and testing all possible configurations and then it says use it to solve the examples given. The examples given were

pot + pan = bib

dog+cat= pig

boy + girl = baby

I keep getting an error saying left hand side of assignment must be a variable

cannot convert from int to bool.

Also in my code where I try to display the output it doesn't run.

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();
            }
}
解决方案

A lot of your problems are stemming from the syntax errors in the code you've written. For example:

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

    • charSet.charAt(subloop) != null is an invalid comparison since the != operator cannot be used for booleans when comparing to null. If you're trying to determine if the characters return from .charAt(var) exist, use parentheses to make independent comparisons of each object.charAt(var) to null.
  2. line 183: charSet = tangible.StringFunctions.changeCharacter(charSet, setIndex++, stringOne.charAt(loop));

    • tangible is ironically not tangible, as the variable does not exist locally or has not been defined globally.
  3. charSet.charAt(setIndex++) = stringTwo.charAt(loop);

    • charSet.charAt(setIndex++) is a method that returns a character. This does not mean you can set the character at the specified index like it's a variable.
  4. line 227: if (exists = 0)

    • You must use == when conducting comparisons in a conditional.
  5. line 269: Scanner scan = new Scanner(System.in);

    • The Scanner class was not imported and thus cannot be used.
  6. line 283: charSet.charAt(maxCharCount) = '\0';

    • Again, you can't use .charAt(var) to set the character at that index like it's a variable.

All of these problems can be self-determined by using a proper IDE, such as Eclipse.

Edit: Try to spend a little more time with pencil and paper working out the logic of your program before writing the code to represent your algorithm. This way you have focus and can write more comprehensive, commented, cleaner code. Here is a bit of a guide to help condense your existing project.

这篇关于有人能帮我清除这些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:51