希望這個有幫助,有點含糊不清...你的意思是,你有幾個字符串< - > int轉換做的?
public YourConstructor() {
/*Map<String, Integer>*/ theMap = new HashMap<String, Integer>();
theMap.put("Pass", 10);
theMap.put("Merit", 70);
theMap.put("distinction ", 90);
...
}
public int getNumber(String text) {
return theMap.get(text);
// I hope this will be, one day, forgotten. This is BAD.
//for (String s : theMap.keySet())
// if (text.equals(s))
// return theMap.get(s);
// return 0; // default for "item not found", or throw
}
從而final String yourString = ""+getNumber(spinner.getSelectedItem().toString());
轉換::字符串整數位是指:
int integer = Integer.parseInt(text);
整數字符串
String theString = ""+integer;
搜索'HashMap' ... –
您只是在測試 「通行證」。你在哪裏有70,80或90? – Stultuske
你應該再次閱讀關於類型的教程 - 這裏似乎有一個誤解,就是類型系統是如何工作的。我們不能告訴你想要做什麼,但一個字符串不是一個整數。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html –