素数递归统计三项代码
public static void isprime() { int n;import java.util.Scanner; public class number { public static void main(String[] args) { int i = 0; System.out.println("1.3-100 2.任意两数 3.最大十个和最小十个"); Scanner sc = new Scanner(System.in); i = sc.nextInt(); switch (i) { case 1: isprime(); break; case 2: isprime1(); break; case 3: isprime2(); break; } } int i = 2; int m = 0; for (n = 3; n >= 3 && n <= 100; n++) { int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; System.out.print(n + "\t"); if (m % 5 == 0) System.out.println(); } } System.out.println(); } public static void isprime1() { System.out.println("请输入两个整数:"); Scanner sc = new Scanner(System.in); int num1, num2; System.out.println("number1:"); num1 = sc.nextInt(); System.out.println("number2:"); num2 = sc.nextInt(); int n; int i = 2; int m = 0; for (n = num1; n >= num1 && n <= num2; n++) { int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; System.out.print(n + "\t"); if (m % 5 == 0) System.out.println(); } } } public static void isprime2() { Scanner sc = new Scanner(System.in); System.out.println("请输入两个整数:"); int num1, num2; System.out.println("number1:"); num1 = sc.nextInt(); System.out.println("number2:"); num2 = sc.nextInt(); System.out.println("十个最小:"); int n; int i = 2; int m = 0; int k = 0; int a[] = new int[100]; for (n = num1; n >= num1 && n <= num2; n++) { int j = 0; if (n % 2 == 0) j = 1; for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) j = 1; } if (j == 0) { m++; a[m] = n; if (m <= 10) System.out.print(a[m] + "\t"); } } System.out.println(); System.out.println("十个最大:"); for (k = m - 10; k <= m; k++) { System.out.print(a[k] + "\t"); } } }
package huiwen; import java.util.Scanner; public class Huiwen { public static void main(String[] args) { String s; int w; int q = 0; Scanner input = new Scanner(System.in); System.out.println("输入一串字母:"); s = input.next(); char a[]; a = s.toCharArray(); w = a.length - 1; for (int k = 0; k < s.length(); k++) { System.out.print(a[k]); } huiwen(a, w, q); } public static void huiwen(char a[], int w, int q) { if (q >= w) { if (a[q] == a[w]) System.out.println("是"); System.exit(0); } if (a[q] == a[w]) huiwen(a, --w, ++q); else { System.out.println("不是!"); System.exit(0); } } }
package j; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; public class lolo{ public static void main(String[] args) throws IOException { String addr="C:/jhl/lol.txt"; BufferedReader bufr = new BufferedReader(new FileReader(addr)); StringBuffer sbuf = new StringBuffer(); String line = null; while ((line = bufr.readLine()) != null) { sbuf.append(line); } bufr.close();// 读取结束 Pattern expression = Pattern.compile("[a-zA-Z]+"); String str1 = sbuf.toString().toLowerCase(); Matcher matcher = expression.matcher(str1); TreeMap myTreeMap = new TreeMap(); int n = 0; Object word = null; Object num = null; while (matcher.find()) { word = matcher.group(); n++;// 单词数加1 if (myTreeMap.containsKey(word)) { num = myTreeMap.get(word); Integer count = (Integer) num; myTreeMap.put(word, count.intValue() + 1); } else { myTreeMap.put(word, new Integer(1)); } } System.out.println("文章内容如下:"); System.out.println(str1); System.out.println("统计分析如下:"); System.out.println("文章中单词总数:" + n+"个"); System.out.println("具体的信息在当前目录的lwl.txt文件中"); BufferedWriter bufw=new BufferedWriter(new FileWriter("C:/jhl/lwl.txt")); Iterator iter=myTreeMap.keySet().iterator(); Object key=null; bufw.write("文件来自:"+addr+"内容如下:"); bufw.write(sbuf.toString()); bufw.newLine(); while(iter.hasNext()){ key=iter.next(); System.out.println(key+":"+myTreeMap.get(key)); bufw.write((String)key+":"+myTreeMap.get(key)); bufw.newLine(); } bufw.write("统计分析如下:"); bufw.write("文章中单词总数:" + n+"个"); bufw.newLine(); bufw.write("文章中不同单词总数:" + myTreeMap.size()+"个"); bufw.close(); } }