⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 occurrencesofdigit.java

📁 这个程序检查一个字符串中的某个数字出现的次数,很不错的
💻 JAVA
字号:

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -