0

我有一个程序,我已经做了很长一段时间了。我需要制作一个程序,通过回溯解决用户指定的求和难题。用户分别输入三个strings,前两个strings加起来应该等于第三个string

例子:

java + next = scala

4656 + 7980 = 12636

我相信我走在正确的轨道上,但我需要获取每个值的索引,<Character> ArrayList并让它们总和小于20,000. 我该怎么做呢?

下面是我到目前为止的代码:

import java.util.*;

public class SummationPuzzle 
{

    public static ArrayList<Character> fsList = new ArrayList<Character>();
    public static ArrayList<Character> lastW = new ArrayList<Character>();
    public static ArrayList<Character> finaList = new ArrayList<Character>();

    /**
     * Reads in 3 words entered by user and converts the first two string into a single <Character> ArrayList
     * takes the third string entered and converts it into it's own <Character>ArrayList
     * @param firstW
     * @param secondW
     * @param thirdW
     */
    public static void convertStr(String firstW, String secondW, String thirdW)
    {
        String combined = firstW + secondW; 
        for(int i = 0; i< combined.length(); i++)
        {
            fsList.add(combined.charAt(i));
        }
        for(int j = 0; j< thirdW.length(); j++)
        {
            lastW.add(thirdW.charAt(j));
        }

        System.out.println( fsList +" "+lastW);
        swapAdd(fsList, lastW);
        //feeds the resulting lists into the swapAdd method
    }

    /**@param
     * This method Swaps the first char of fsList with the char at fsList[1]to make sure it matches the char at lastW[1]
     * @param fsList
     * @param lastW
     */
    public static void swapAdd(ArrayList<Character> fsList, ArrayList<Character> lastW)
    {
        Collections.swap(lastW, 0,1);
        System.out.println(lastW + " lastW swap first char");
        char temp = lastW.get(1);
        int j= 0;
        System.out.println(fsList+ " before swap");
        if(!fsList.get(1).equals(temp) && fsList.contains(temp))
        {
            j = fsList.indexOf(temp);
            Collections.swap(fsList,1,j);
        }
        System.out.println(fsList+ " after swap");
        removeDuplicate(fsList, lastW);
    }

    /**
     * Combines two <Character> ArrayList into a one <Character> ArrayList with single instances of the char
     * @param fsList
     * @param lastW
     */
    public static void removeDuplicate(ArrayList<Character> fsList, ArrayList<Character> lastW)
    {
        ArrayList<Character> tempList = new ArrayList<Character>();
        tempList.addAll(fsList);
        tempList.addAll(lastW);
        for(char dupLetter : tempList)
        {
            if(!finaList.contains(dupLetter))
            {
                finaList.add(dupLetter);
            }
        }
        System.out.println(finaList + "This is the list with duplicates removed");
        System.out.println(lastW);

    }


    //main method
    public static void main(String[] args)
    {
        //Receive user input
        Scanner userIn = new Scanner(System.in);
        System.out.println("Please enter your first word");
        String firstW = userIn.next().trim();
        System.out.println("Please enter your Second word");
        String secondW = userIn.next().trim();
        System.out.println("Please enter your Third word");
        String thirdW = userIn.next().trim();


        //print the summation puzzle
        System.out.println(firstW+ " + " + secondW + " = "+ thirdW);
        convertStr(firstW, secondW, thirdW);
    }
}
4

0 回答 0