我正在尝试为每个角色分配一个数字

时间:2016-04-07 15:01:19

标签: java recursion data-structures

import java.util.Scanner;
public class Recursion
{
    //variables to hold string values
    public static String s1 = new String(new char[10]);
    public static String s2 = new String(new char[10]);
    public static String s3 = new String(new char[11]);
    public static String charSet = new String(new char[11]);
    //variables to hold number values
    public static int numberOne;
    public static int numberTwo;
    public static int numberThree;
    public static int maxCharCount;
    public static int[] numberSet = new int[10];
    //function which generates a number
    public static void checkForEquality()
    {
        numberOne = numberTwo = numberThree = 0;
        int i;
        int j;
        for (i = 0; i < s1.length(); i++)
        {
            for (j = 0; j < maxCharCount; j++)
            {
                if (s1.charAt(i) == charSet.charAt(j))
                {
                    if (i == 0 && numberSet[j] == 0)
                    return;
                    //generate the number
                    numberOne = (numberOne * 10) + numberSet[j];
                }
            }
        }
        for (i = 0; i < s2.length(); i++)
        {
            for (j = 0; j < maxCharCount; j++)
            {
                if (s2.charAt(i) == charSet.charAt(j))
                {
                    if (i == 0 && numberSet[j] == 0)
                    return;
                    //generate number
                    numberTwo = (numberTwo * 10) + numberSet[j];
                }
            }
        }
        for (i = 0; i < s3.length(); i++)
        {
            for (j = 0; j < maxCharCount; j++)
            {
                if (s3.charAt(i) == charSet.charAt(j))
                {
                    if (i == 0 && numberSet[j] == 0)
                    return;
                    //generate the number
                    numberThree = (numberThree * 10) + numberSet[j];
                }
            }
        }
    }
    public static void display(){
        if (numberOne + numberTwo == numberThree) {
            //display the output
            int i=0;
            System.out.println();
            System.out.print(" Summation Puzzle solved. ");
            System.out.print("n");
            System.out.print(s1);
            System.out.print("<==>");
            System.out.print(numberOne);
            System.out.print("n");
            System.out.print(s2);
            System.out.print("<==>");
            System.out.print(numberTwo);
            System.out.print("n");
            System.out.print(s3);
            System.out.print("<==>");
            System.out.print(numberThree);
            System.out.print("n");
            //loop to show the result
            for (i = 0; i < maxCharCount; i++)
            {
                System.out.println(charSet.charAt(i));
                System.out.print("<==>");
                System.out.print(numberSet[i]);
                System.out.print("n");
            }
            System.exit(0);
        }
    }
    //recursive function which will call itself
    public static void Combinations(int indexCounter, int[] availableSet)
    {
        int i;
        if (indexCounter != 0)
        {
            for (i = 0; i < 10; i++)
            {
                numberSet[indexCounter] = i;
                if (availableSet[i] == 1)
                {
                    availableSet[i] = 0;
                    Combinations(indexCounter + 1, availableSet);
                    availableSet[i] = 1;
                }
            }
        }
        if (indexCounter == maxCharCount)
        checkForEquality();
    }
    public static void createCharSet()
    {
        int i;
        int setIndex;
        int present;
        int j;
        setIndex = 0;
        for (i = 0; i < s1.length(); i++)
        {
            present = 0;
            for (j = 0; j < setIndex; j++)
            {
                if (s1.charAt(i) == charSet.charAt(j))
                {
                    present = 1;
                }
            }
            if (present == 0)
            {
                charSet = StringFunctions.changeCharacter(charSet, setIndex++, s1.charAt(i));
            }
        }
        for (i = 0; i < s2.length(); i++)
        {
            present = 0;
            for (j = 0; j < setIndex; j++)
            {
                if (s2.charAt(i) == charSet.charAt(j))
                {
                    present = 1;
                }
            }
            if (present == 0)
            {
                charSet = StringFunctions.changeCharacter(charSet, setIndex++, s2.charAt(i));
            }
        }
        for (i = 0; i < s3.length(); i++)
        {
            present = 0;
            for (j = 0; j < setIndex; j++)
            {
                if (s3.charAt(i) == charSet.charAt(j))
                {
                    present = 1;
                }
            }
            if (present == 0)
            {
                charSet = StringFunctions.changeCharacter(charSet, setIndex++, s3.charAt(i));
            }
        }
        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;
            }
            Combinations(0, avaliableSet);
        }
    }
    //main method
    public static void main(String[]args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the first String :");
        s1 = scan.next();
        System.out.print("Enter the second String :");
        s2 = scan.next();
        System.out.print("Enter the thirsd String :");
        s3 = scan.next();
        createCharSet();
        System.out.print(" result of your 3 three strings = ");
        System.out.print(charSet);
        calculateSummation();
        checkForEquality();
        display();
    }
}

每次运行程序时,它只为每个值赋值0。我希望能够为每个非数字字符分配1-10的值。 有人能帮助我吗?

看起来应该是这样的 Sample output

1 个答案:

答案 0 :(得分:0)

首先,如果正确格式化代码,您可能会发现调试更容易。我记得你昨晚发布的帖子,你遇到了类似的语法问题。如果您将代码格式化为这样,您可能会发现代码更容易阅读:

//function which generates a number
public static void checkForEquality(){
    numberOne = numberTwo = numberThree = 0;
    int i;
    int j;
    for (i = 0; i < s1.length(); i++){
        for (j = 0; j < maxCharCount; j++){
            if (s1.charAt(i) == charSet.charAt(j)){
                if (i == 0 && numberSet[j] == 0)
                    return;

               //generate the number   
                numberOne = (numberOne * 10) + numberSet[j];
            }
        }
    }

    for (i = 0; i < s2.length(); i++){
        for (j = 0; j < maxCharCount; j++){
            if (s2.charAt(i) == charSet.charAt(j)){
                if (i == 0 && numberSet[j] == 0)
                    return;

               //generate number  
               numberTwo = (numberTwo * 10) + numberSet[j];
            }
        }
    }

查看代码的这一部分,看看它是否仍然正确地遵循您的算法。您可能会发现错误,您可以使用格式正确的代码找到自己的错误。如果你看看你的previous question(我已经回答),你会找到一个有用的链接,指导你编写更好的代码。

另外,我没有尝试在我自己的IDE中编译它,但你应该看看Eclipse。您可以自动定位许多小错误。