occurrencesofdigit.java
来自「这个程序检查一个字符串中的某个数字出现的次数,很不错的」· Java 代码 · 共 36 行
JAVA
36 行
import javax.swing.JOptionPane;
public class OccurrencesOfDigit{
public static void main (String[] args){
String s= JOptionPane.showInputDialog(null,"Enter the string please!","Occurrences of digits",JOptionPane.QUESTION_MESSAGE);
int[] count = count (s);
for (int j = 0;j<=9;j++){
if (count[j]==0||count[j]==1)
System.out.println(j+ " occurred " +count[j]+" time");
else
System.out.println(j+ " occurred " +count[j]+" times");
}
}
public static int[] count(String s){
int[] counts = new int[10];
for (int i = 0;i< s.length();i++ ){
if (s.charAt(i)<='9'&&s.charAt(i)>='0')
counts[s.charAt(i)-'0']++;
}
return counts;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?