我有一個家庭作業,需要我編寫一個程序來計算輸入行中的點。到目前爲止,這是我所想出來的(有點),只不過它是在計算一切,而不是點數。我堅持如何讓程序只計算點數。點計數器問題
import javax.swing.*;
import java.lang.Character;
public class Assign5_Polk {
public static void main(String[] args) {
String string = JOptionPane.showInputDialog("Give me dots and i will count them : ");
int count = 0;
for (int i = 0; i< string.length(); i++) {
char c = string.charAt(i);
if (string.contains(".")) {
count++;
}
}
System.out.println("There are" + " "+ count + " " + "dots" +" " + "in this string. " + string);
}
}
由於焦炭' '是原始的,你只需要'c =='。'',而不是「.equals」。 –
char沒有等於方法 – Kick
如果您編輯您的帖子,downvoters可能會刪除他們的不良投票。 – csmckelvey