素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.*; public class PrimenumberManager { private int output_count=0; //记每行的输出数 private int min = 3; private int max = 100; public void calculation(){ //计算范围内的素数 System.out.println(min+"到"+max+"之间的素数有:"); for( int i=min; i<=max; i++){ int sign=0; //标记 for( int j=2; (j<Math.sqrt(i))&&(sign==0); j++){ if((i%j)==0){ sign++; } } if(sign==0){ display(i); } } output_count = 0; System.out.println(); } public void display( int number ){ System.out.print(number); output_count++; if( output_count == 5){ //一行有五个数字,换行 System.out.println(); output_count=0; } else{ System.out.print(" "); } } @SuppressWarnings("resource") public void setMinMax(){ System.out.println("请输入两个整数"); Scanner in = new Scanner( System.in ); int min = in.nextInt(); int max = in.nextInt(); if( max < min){ max+= min; min = max-min; max = max-min; } this.min = min; this.max = max; calculation(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* * 目的:计算范围内素数 * 作者:刘雨馨 * 日期:2018.10.14 */ public class PrimeNumber { public static void main( String [] args ){ PrimenumberManager p = new PrimenumberManager(); p.calculation(); p.setMinMax(); } }
回文
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Detection { private int sign=0; private int length; private String str = new String(); public void setStr( String str ){ int n=0; this.str = str; length = str.length(); judge(n); } public void judge( int n ){ if((length-n)<=1){ //字符串是零或单个字符回文 sign=1; } else{ int front = str.charAt(n); int back = str.charAt(length-n-1); if( front == back ){ judge(++n); } else{ sign=0; } } } public void display(){ if( sign == 0) System.out.println("该字符串不回文"); else System.out.println("该字符串回文"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; public class Palindrome { public static void main( String [] args ){ Detection d = new Detection(); @SuppressWarnings("resource") Scanner in = new Scanner( System.in ); String str = new String(); System.out.println("请输入字符串"); str = in.next(); d.setStr(str); d.display(); } }
单词频率
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.io.*; public class Statistics { private String word=""; private String[] str = new String[500]; private int[] count = new int[500]; private int i=0; //统计现有单词的数量 //读取文件内容 public void readFile( String filename ){ File file = new File( filename ); Reader reader = null; try{ reader = new InputStreamReader( new FileInputStream( file ) ); int tempchar; while((tempchar = reader.read()) != -1 ){ extractWord( tempchar ); //传每个字符 } reader.close(); } catch( Exception e ){ e.printStackTrace(); } } //提取单词 public void extractWord( int tempchar ){ if( ((tempchar>65&&tempchar<90)||(tempchar>97&&tempchar<122)) ){ //tempchar为字母时,追加 word += (char)tempchar; } else if((!"".equals(word))){ statisticalWord(); //统计 word=""; } } //统计单词 public int statisticalWord(){ int j=0; for( ; j<i; j++ ){ if( str[j].equals(word) ){ count[j]++; //计次加一 return 0; } } str[j]=word; //添加单词 count[j]++; i++; return 0; } //将统计信息显示 public void display(){ for(int j=0; j<i; j++ ){ System.out.println(str[j]+"\t"+count[j]); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Words { public static void main( String [] args ){ String filename = "C:/file.txt"; Statistics sta = new Statistics(); sta.readFile(filename); sta.display(); } }