好的,所以我正在做這個項目,要求我有一個字符串的第一個和最後一個setters出現與他們之間計算的數字和輸出。我試過重新調整一些反向字符串代碼,但我無法讓輸出顯示在我的IDE中。計數字母之間的第一個和最後 - Java
任何人都可以看看我的代碼,並提出一些建議?
public static void main(String[] args) {
String countWord;
countWord = JOptionPane.showInputDialog(null,
"Enter the string you wish to have formatted:");
}
static String countMe(String countWord) {
int count = 1;
char first = countWord.charAt (0);
char last = countWord.charAt(-1);
StringBuilder word = new StringBuilder();
for(int i = countWord.length() - 1; i >= 0; --i)
if (countWord.charAt(i) != first) {
if (countWord.charAt(i) != last) {
count++;
}
}
return countWord + first + count + last;
}
}
什麼是「字符串二傳手」? – Rainbolt
我想你想char last = countWord.charAt(countWord.length - 1);,爲什麼不只是使用countWord.length - 2來得到中間的字符數? –
我不明白你想要做什麼,也不知道你有什麼問題。你能否澄清兩個? – Keppil