【作业】随机数+参数可变的方法+实验任务(输出素数+使用递归,判断是否为回文+统计一篇英语问斩单词出现频率)

(1)随机数

取余的m很大可以保证随机的更彻底,更不容易重复

 1 import java.util.Scanner;
 2 public class Text_001
 3 {
 4 
 5     public static void main(String[] args)
 6     {
 7         Scanner input=new Scanner(System.in);
 8         int x1;//x[n-1]
 9         int x2;//x[n]
10         int a=16807;//Multiplier=75=16807
11         int c=0;
12         int m=2001;//Modulus=2^31-1=int.MaxValue  当显示过231-2个数之后,才可能重复
13         int n=0;
14         int num=1000;//数量
15         System.out.println("请输入数量");
16         num=input.nextInt();        
17         for(n=1,x1=a;n<=num;n++)
18         {
19             x2=(a*x1+c)%m;
20             System.out.print(x2+"\t");
21             if(n%5==0)System.out.println();
22             x1=x2;
23         }
24     }
25 }
Random

(2) 

课件代码示例

 1 // MethodOverload.java
 2 // Using overloaded methods
 3 
 4 public class MethodOverload {
 5 
 6     public static void main(String[] args) {
 7         System.out.println("The square of integer 7 is " + square(7));
 8         System.out.println("\nThe square of double 7.5 is " + square(7.5));
 9     }
10 
11     public static int square(int x) {
12         return x * x;
13     }
14 
15     public static double square(double y) {
16         return y * y;
17     }
18 }
MethodOverload.java

println是一个方法,参数数量可变

原因……看了看api,找到了一下两行,很明显有省略号

PrintStream printf(Locale l, String format, Object... args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
PrintStream printf(String format, Object... args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.

(3)输出素数

 1 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 public class Sushu
 4 {
 5 
 6     public static void main(String[] args)
 7     {
 8         ArrayList<Integer> num=new ArrayList<Integer>();
 9         Scanner input=new Scanner(System.in);
10         int min=3,max=100;
11         System.out.println("请输入两个整数");
12         min=input.nextInt();
13         max=input.nextInt();
14         num=Judge(min, max);
15         show(num);
16     }
17     public static void show(ArrayList<Integer> num)
18     {
19         int k=0,i=0;
20         //所有
21         System.out.println("遍历");
22         for(i=0;i<num.size();i++)
23         {
24             System.out.print(num.get(i)+"\t");
25             k++;
26             if(k==5)
27             {
28                 k=0;
29                 System.out.println();
30             }
31         }
32         //最大
33         System.out.println("\n最大");
34         for(i=0,k=0;i<10&&i<num.size()&&num.size()-i>=0;i++)
35         {
36             System.out.print(num.get(num.size()-i-1)+"\t");
37             k++;
38             if(k==5)
39             {
40                 k=0;
41                 System.out.println();
42             }
43         }
44         //最小
45         System.out.println("\n最小");
46         for(i=0,k=0;i<10&&i<num.size();i++)
47         {
48             System.out.print(num.get(i)+"\t");
49             k++;
50             if(k==5)
51             {
52                 k=0;
53                 System.out.println();
54             }
55         }        
56     }
57     public static ArrayList<Integer> Judge(int min,int max) 
58     {
59         int i=0,t=0,n=0,f=0;
60         ArrayList<Integer> num=new ArrayList<Integer>();
61         for(i=min;i<=max;i++)
62         {
63             n=(int)(Math.sqrt((double)i));
64             for(t=2,f=0;t<=n;t++)
65             {
66                 if(i%t==0)
67                 {
68                     f=1;
69                     break;
70                 }
71             }
72             if(f==0)num.add(i);
73         }
74         return num;
75     }
76 }
Sushu

(4)使用递归,判断是否为回文

就像剥洋葱一样,一层层往下剥。最后剩下一个,或一个不剩,说明是回文

 1 import java.util.Scanner;
 2 public class Palindrome
 3 {
 4 
 5     public static void main(String[] args)
 6     {
 7         Scanner input=new Scanner(System.in);
 8         String s="我是谁是我";
 9         String t="";
10         System.out.println("请输入一个句子");
11         s=input.nextLine();
12         t=fun(s);
13         if(t.length()<=1)
14         {
15             System.out.println(s+"   是回文");
16         }
17         else
18         {
19             System.out.println("不是回文");
20         }
21     }
22     public static String fun(String s)
23     {
24         if(s.length()>1&&s.charAt(0)==s.charAt(s.length()-1))
25         {
26             s=s.substring(1, s.length()-1);
27             return fun(s);
28         }
29         else
30         {
31             return s;
32         }
33     }
34 }
Palindrome

(5)统计一篇英语问斩单词出现频率(从文件中读取)

一个一个字符分析,如果出现字母,再次出现不是字母的,那么值钱的字母记为一个单词,存到字符串内

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 import java.io.*;
  5 
  6 public class StatisticsWord
  7 {
  8     public static void main(String[] args)
  9     {
 10         String fileName="e1.txt",fileName2="e4.txt";
 11         String line="";
 12         String word="";
 13         int p=0;
 14         int lineLength=0;
 15         int f1=0,f2=0;
 16         int sum=10;
 17         char c;
 18         Scanner input=new Scanner(System.in);
 19         ArrayList<String> wordRecoed=new ArrayList<String>();
 20         ArrayList<Integer> wordNum=new ArrayList<Integer>();
 21         System.out.println("请输入读取的文件名");
 22         fileName=input.nextLine();
 23         System.out.println("请输入用于储存的文件名");
 24         fileName2=input.nextLine();
 25         System.out.println("请输入数量");
 26         sum=input.nextInt();        
 27         try
 28         {
 29             FileReader fr=new FileReader(fileName);
 30             Scanner in=new Scanner(fr);
 31             //读取
 32             while(in.hasNextLine())
 33             {
 34                 line=line+in.nextLine()+"\n";
 35             }
 36             in.close();
 37             fr.close();
 38             lineLength=line.length();
 39             //System.out.println(line+" "+lineLength);
 40             while(p<lineLength)
 41             {
 42                 f1=0;
 43                 f2=0;
 44                 word="";
 45                 //拆分
 46                 do
 47                 {
 48                     c=line.charAt(p++);
 49                     //System.out.print(c+"  |  ");
 50                     if(c==' '||c=='\n')
 51                     {
 52                         if(f1==1)
 53                         {
 54                             f2=1;
 55                         }
 56                         break;
 57                     }
 58                     else
 59                     {
 60                         if((c>='a'&&c<='z')||c>='A'&&c<='Z')
 61                         {
 62                             f1=1;
 63                             word=word+c;
 64                         }
 65                         else
 66                         {
 67                             if(f1==1)
 68                             {
 69                                 f2=1;
 70                             }
 71                             break;
 72                         }
 73                     }
 74                     
 75                 }while(p<lineLength);
 76                 if(f2==1)
 77                 {
 78                     //对比
 79                     Compare(wordRecoed,wordNum,word);
 80                 }
 81             }    
 82             //排序
 83             Sort(wordRecoed, wordNum);
 84             for(int x=0;x<sum;x++)
 85             {
 86                 System.out.println(wordRecoed.get(x)+" "+wordNum.get(x));
 87             }
 88             
 89             write(wordRecoed,wordNum,fileName2,sum);
 90         }
 91         catch(Exception e)
 92         {
 93             e.printStackTrace();
 94         }
 95         
 96     }
 97     public static void write(ArrayList<String> wordRecoed,ArrayList<Integer> wordNum,String s,int sum)
 98     {
 99         int i=0;
100         try
101         {
102             File f=new File(s);
103             if(!f.exists())
104             {
105                 f.createNewFile();
106             }
107             FileWriter fr=new FileWriter(f);
108             PrintWriter pw=new PrintWriter(fr);
109             for(i=0;i<sum;i++)
110             {
111                 pw.println(wordRecoed.get(i)+" "+wordNum.get(i));
112             }
113             pw.flush();
114             pw.close();
115             fr.close();
116             
117         }
118         catch(Exception e)
119         {
120             e.printStackTrace();
121         }
122     }
123     public static void Sort(ArrayList<String> wordRecoed,ArrayList<Integer> wordNum)
124     {
125         int l=wordRecoed.size();//长度
126         int i,t,k,j;
127         String s1,s2;
128         for(i=0;i<l-1;i++)
129         {
130             for(t=0;t<l-i-1;t++)
131             {
132                 if(wordNum.get(t)<=wordNum.get(t+1))
133                 {
134                     //交换
135                     k=wordNum.get(t);
136                     j=wordNum.get(t+1);
137                     wordNum.set(t+1, k);
138                     wordNum.set(t, j);
139                     s1=wordRecoed.get(t);
140                     s2=wordRecoed.get(t+1);
141                     wordRecoed.set(t+1, s1);
142                     wordRecoed.set(t, s2);
143                 }
144             }
145         }
146     }
147     public static void Compare(ArrayList<String> wordRecoed,ArrayList<Integer> wordNum,String word)
148     {
149         int l=wordRecoed.size();//长度
150         int i=0,t=0;
151         int flag1=0;
152         for(i=0;i<l;i++)
153         {
154             if(wordRecoed.get(i).equals(word))
155             {
156                 t=wordNum.get(i)+1;
157                 wordNum.set(i, t);
158                 flag1=1;
159                 break;
160             }
161         }
162         if(l==0||flag1==0)
163         {
164             wordRecoed.add(word);
165             wordNum.add(1);
166         }
167     }
168 }
View Code

猜你喜欢

转载自www.cnblogs.com/26never/p/9787073.html