1. 程式人生 > >第二次實驗及動手動腦

第二次實驗及動手動腦

集合 自然 exti boolean ont 鍵值 http next() 作文

//何偉豪     2018/10/14
//實現任意兩個數之間的素數輸出

package 素數輸出;

import java.util.*;


public class 素數輸出 {
    public static void main(String [] arge) {
        Scanner in = new Scanner(System.in);
        int number1,number2;   //輸入的兩個數
        int sum5=1;            //判斷是否該換行的變量
        int opt=0;             //記錄是否為素數的變量
int n; //動態數組 int m=0; System.out.println("請輸入兩個大於等於三的整數"); number1 = in.nextInt(); //輸入前限 number2 = in.nextInt(); //輸入後限 n=number2-number1; //給動態數組中的n賦值 int[] daxiao=new int[n]; System.out.println(number1 + "~" + number2 + "間的所有素數如下:");
//判斷兩數間的每個數是否為素數的循環 for(int i=number1;i<=number2;i++) { double ii=Math.sqrt(i); //從2開始判斷是否出現除不盡的數 for(int o=2;o<=ii+1;) { //如果除盡的話 if(i%o==0) { o=number2; opt=0; }
//除不盡的話 else { o++; opt=1; } } if(opt==1) { if(sum5<5) { sum5++; System.out.print(i + " "); daxiao[m]=i; m++; }else { sum5=1; System.out.println(i + " "); daxiao[m]=i; m++; } } } System.out.println(); if(m<10) { System.out.println("你所輸入的兩數之間素數不夠十個!"); }else { System.out.println("最小的十個素數如下:"); for(int qshi=0;qshi<10;qshi++) { System.out.print(daxiao[qshi] + " "); } System.out.println(); System.out.println("最大的十個素數如下:"); for(int hshi=m-1;hshi>m-11;hshi--) { System.out.print(daxiao[hshi] + " "); } } in.close(); } } //何偉豪 2018/10/14 //判斷某個字串是否是回文 package 字符串回文; import java.io.BufferedReader; import java.io.InputStreamReader; public class 字符串回文 { public static void main(String[] args)throws Exception { String shuru =""; BufferedReader zfc = new BufferedReader(new InputStreamReader(System.in),256); System.out.println("請輸入字符串:"); shuru = zfc.readLine(); System.out.println("檢查結果: " + huiwen(shuru,0,shuru.length()-1)); } public static boolean huiwen(String s,int start,int end){ if(start == end) return true; if(start > end){ System.out.println("您沒有輸入任何字符串!"); return false; } if(s.charAt(start) == s.charAt(end)){ return huiwen(s,start+1,end-1); } else{ return false; } } } //何偉豪 2018/10/14 //查相同的字母有多少個 package 論文查重; import java.io.*; import java.util.*; public class 查重 { public static void main(String[] args){ demo(new File("C:\\Users\\Lenovo\\Desktop\\JAVA\\臨時文件\\論文查重\\論文查重\\作文.txt")); } public static void demo(File file){ BufferedReader bfr = null; //定義字符讀取(緩沖)流 try{ bfr = new BufferedReader(new FileReader(file));//給該流賦值 String value = null; //定義一個臨時接收文件中的字符串變量 String newValue = ""; //接收文件中所有字符串的變量 while((value = bfr.readLine())!=null){ //開始讀取文件中的字符 newValue = newValue+value; //存入newValue變量中 } char[] ch = newValue.toCharArray();//把newValue變成字符數組 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(Collections.reverseOrder());/*定義一個TreeMap(因為TreeMap是有序的,存入的鍵值都有自然比較順序功能,默認的是從小到大順序,所有這裏傳了一個反轉的比較器),鍵對應字符,值對應字符出現的次數**/ for(int x = 0;x<ch.length;x++){ //遍歷ch 將ch中所有的字符存入一個Map集合中(TreeSet),鍵對應字符,值對應字符出現的次數 char c = ch[x]; if(tm.containsKey(c)){ //如果TreeMap(tm)中有該鍵,則取出該鍵中的值,也就是出現的次數 int conut = tm.get(c); tm.put(c,conut+1); //存入把新值存入tm集合中,如果鍵相同的話, 新鍵會替換老鍵,值也隨著變化了 } else{ tm.put(c, 1); //如果沒有出現該鍵就說明是第一次出現,然後就存入1次 } } //下面的是取出TreeMap(tm)中的鍵和值 Set<Map.Entry<Character, Integer>> set = tm.entrySet(); Iterator<Map.Entry<Character, Integer>> iter = set.iterator(); while(iter.hasNext()){ Map.Entry<Character, Integer> map = iter.next(); char k = map.getKey(); int v = map.getValue(); System.out.print(k+"("+v+") "); } } catch(IOException e){ System.out.println("文件讀取錯誤"); } finally{ try{ if(bfr!=null) bfr.close(); } catch(IOException e){ System.out.println("文件關閉錯誤"); } } } } 動手動腦: // MethodOverload.java // Using overloaded methods public class MethodOverload { public static void main(String[] args) { System.out.println("The square of integer 7 is " + square(7)); System.out.println("\nThe square of double 7.5 is " + square(7.5)); } public static int square(int x) { return x * x; } public static double square(double y) { return y * y; } }

技術分享圖片

第二次實驗及動手動腦