ハッシュマップから値とキーを入れ替える方法と、ユーザーがキーボードからstringbuilderを出し、stringbuilderの1ワードをjavaの新しい値に置き換えることはできますか?Ι逆方向変換スラングインターネットの辞書を作成したい私はこの文字列にtsanlationの後に "笑い声"を与えました。文字列 "loI"が必要です。私の辞書であり、この値{fyi "、"あなたの情報 "}を含むhasmapを作成しました; ( "笑"、 "笑い声");また、ユーザーが "笑い声"を入力するとアウトとlound "笑」oytputために取る"ハーフマップからの値とキーのスワップ
私のコードは次のとおりです。 パッケージjavaapplication6;
。パブリッククラスJavaApplication6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String threeWords;
//create stringbuilder
StringBuilder acronym = new StringBuilder();
Scanner scan = new Scanner(System.in);
//o xrhsths dinei eisodo thn protash poy thelei na metafrasei
System.out.println("Eisigage thn protasi sou pou theleis na metafraseis:\n ");
threeWords = scan.nextLine();
threeWords = threeWords.toLowerCase(); //Changing it to lower case
acronym.append(threeWords);
System.out.println(" To string pou eisigages einai: " + acronym +"\n");
//dhmiougroum ena pinaka pou periexei mia mia thn lejei jexorista apo thn protash pou
//edose o xrhsths oste na mporoume na broume pio eukola thn leji pou einai gia metafrash sto
//lejiko
String[] threeWordsArray = threeWords.split(" ");
//dhmiougria lejikou me thn slang tou internet me thn xrisi hashmap
HashMap<String,String> dictionary = new HashMap <String,String>(10);
//Prosthiki lejeon sto lejiko mas
dictionary.put("fyi", "for your information");
dictionary.put("dae", "Does anyone else");
dictionary.put("thx","thank you");
dictionary.put("omg","Oh my god");
dictionary.put("lol","laughing out lound");
HashMap<String, String> reversedHashMap = new HashMap<String, String>();
for (String key : dictionary.keySet()){
reversedHashMap.put(dictionary.get(key), key);
}
System.out.println("reversedHasmap"+reversedHashMap);
for(String word : threeWordsArray) {
if (reversedHashMap.containsKey(word)) {
String definition = reversedHashMap.get(word);
System.out.println("h metafrash einai: \n" + definition);
int index=acronym.indexOf(word);
System.out.println("index:"+index);
acronym.insert(index,definition);
}
else {
System.err.println("Word not found");
}//end if
}//end for
System.out.println("To string metafrasmeno einai: " + acronym);
}
}
が、私は問題を持っている(文字列ワード:threeWordsArray)用 用{
if (reversedHashMap.containsKey(word)) {
String definition = reversedHashMap.get(word);
System.out.println("h metafrash einai: \n" + definition);
int index=acronym.indexOf(word);
System.out.println("index:"+index);
acronym.insert(index,definition);
}
else {
System.err.println("Word not found");
}//end if
}//end for
これは、ある入力を見つけることができませんreverseHashMapキーと一致します。
あなたが望むのは正確に何ですか、わかりません?これまで何をしていますか? – Asew
何ですか?いくつかの入力+出力とこれまでに試したことを示してください。 – luk2302
StringBuilderがキーボードを使用している間は、キーを戻してからキーを戻す前に値を配置します。簡単です! – Michael