我有這種方法應該檢查一個鍵是否在地圖中,然後增加一個值。檢查地圖中的鍵
papers.add(paper); //add a paper to the paper collection
papersOrdered.get(papers); //returns the papers
if(!(papersOrdered.containsKey(paper))) {
paperNo = 1;
String noPaper = Integer.toString(paperNo); //convert the paperNo integer to a String
papersOrdered.put(papers,noPaper); //put the rooNo and the paper collection into the HashMap
System.out.println("'" + paper + "'" + " has been added to the paper order. " + noPaper + " copy ordered"); //a message to say a paper has been added
}
else{
paperNo = paperNo ++;
String noPaper = Integer.toString(paperNo); //convert the paperNo integer to a String
papersOrdered.put(papers,noPaper); //put the rooNo and the paper collection into the HashMap
System.out.println("'" + paper + "'" + " has been added to the paper order." + noPaper + " copies ordered"); //a message to say a paper has been added
}
System.out.println("======================================================="); ; //a line to seperate text
System.out.println(" "); //a blank line
但它不起作用,我不知道爲什麼?
它以什麼方式不起作用? –
如果您輸入已經在地圖中的鍵,則數字不會增加。 –
'paperNo'從哪裏來?看起來你的問題是'paperNo'不是你從地圖中獲得的數字。 (另外,你爲什麼要將整數轉換成字符串和從字符串轉換整數,當你只能使用整數時?) –