私はいくつかのJavaコードについて説明しています:
これらのコードの違いは何ですか。一方はコンパイルできますが、もう一方はコンパイルできません。
「エラーを修正する方法」はすでに解決しているので興味はありませんが、問題の説明については詳しく説明します。
働く
public void x(){
HashMap<String , Integer> count= new HashMap<String, Integer>();
Scanner scan= new Scanner("hdsh");
String tmp;
while((tmp=scan.next())!=null){
if(count.containsKey(tmp)){
count.put(tmp, 1);
}
else{
count.put(tmp, count.get(tmp)+1);
}
tmp=scan.next();
}
}
動作しない
public void x(){
HashMap<String , Integer> count= new HashMap<String, Integer>();
Scanner scan= new Scanner("hdsh");
while((String tmp=scan.next())!=null){
if(count.containsKey(tmp)){
count.put(tmp, 1);
}
else{
count.put(tmp, count.get(tmp)+1);
}
tmp=scan.next();
}
}
Eclipseのエラーは次のとおりです。
この行の複数のマーカー:
- 文字列を変数に解決できません
- トークン「tmp」の構文エラー、このトークンを削除します
- 文字列を変数に解決できません
- トークン「tmp」の構文エラー、このトークンを削除します