Java 如何求和指数';a<;性格>;ArrayList
我有一个程序,我已经工作了一段时间了。我需要做一个程序,通过回溯解决用户指定的求和难题。 用户输入三个单独的Java 如何求和指数';a<;性格>;ArrayList,java,arraylist,character,type-conversion,compare-and-swap,Java,Arraylist,Character,Type Conversion,Compare And Swap,我有一个程序,我已经工作了一段时间了。我需要做一个程序,通过回溯解决用户指定的求和难题。 用户输入三个单独的字符串,前两个字符串加在一起应等于第三个字符串 例如: java + next = scala 4656 + 7980 = 12636 我相信我的思路是正确的,但我需要在ArrayList上获取每个值的索引,并使它们的总和小于20000。我该怎么做呢 以下是我目前掌握的代码: import java.util.*; public class SummationPuzzle {
字符串
,前两个字符串
加在一起应等于第三个字符串
例如:
java + next = scala
4656 + 7980 = 12636
我相信我的思路是正确的,但我需要在
ArrayList
上获取每个值的索引,并使它们的总和小于20000
。我该怎么做呢
以下是我目前掌握的代码:
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);
}
}
import java.util.*;
公开课总结难题
{
public static ArrayList fsList=new ArrayList();
公共静态ArrayList lastW=新ArrayList();
public static ArrayList finaList=new ArrayList();
/**
*读入用户输入的3个单词,并将前两个字符串转换为单个ArrayList
*获取输入的第三个字符串并将其转换为自己的ArrayList
*@param firstW
*@param secondW
*@param thirdW
*/
公共静态void convertStr(字符串firstW、字符串secondW、字符串thirdW)
{
组合字符串=firstW+secondW;
对于(int i=0;i
您如何将“java”映射为“4656”等等?在什么逻辑中?那是我的问题映射不是一个选项。所以我必须给每个字符分配一个无映射的数值。我不知道该怎么做。