10.11JAVA作业

[实验任务]素数输出

1、 实验要求:

1)编写判断该数是否为素数的方法,并在主方法中对其进行调用。

2)注意编程规范:程序开头部分的目的,作者以及日期;必要的空格与缩进,适当的注释等;

3)实验报告中要求包括程序设计思想、程序流程图、源代码、运行结果截图、编译错误分析等内容。

2、   实验内容

1)计算并输出3~100之间的素数。

2)编程满足下列要求:

 1)按照每行5个输出;

 2)输出任意两个整数之间的所有素数;

 3)输入两个整数,输出这两个整数之间的最大的10个和最小的10个素数。

代码

1 思想

利用创建扫描器实现输入,判断素数运用了布尔返回值。再判断素数函数中运用强制类型转换实现数的开方变整数INT型,2直接返回真值,其他的开方如果在最大数为开方值的循环中输入数number除以i求余为0则返回false计算完都没有0则返回true。利用函数判断数是否为素数加numam实现计数最后输出完。在输出一遍前十个数和后10个数。

2 流程图

 

       

package YANG;//王重阳 信1705-2 20173600

import java.util.Scanner;

public class YANG535 {

public static void main(String[] args) {

System.out.println("下面进行随机输入两个整数求其中的素数以及其中最大和最小的各10个"+'\n');

Scanner scan=new Scanner(System.in);

System.out.println("请输入两个整数");

int A=scan.nextInt();

int B=scan.nextInt();

 int i,count=0,j=0,num=0,am=0,k=0;

for( i=A; i<=B; i++){

if(isPrimeNumber(i) == true){

count++;

System.out.printf("%6d", i);

if(count%5 == 0)

System.out.println();

                            }

                     }

System.out.println("其中最大和最小的10个素数各是");

System.out.println();

for(k=A;k<=B;k++) {

if(isPrimeNumber(k)==true)

{am++;} }

for(j=A;j<=B;j++) {

if(isPrimeNumber(j)==true) {

num++;

if((num/10==0)||(num>am-10))

{System.out.printf("%6d", j);}

}

 }

}

//判断一个数是否是素数,若是,返回true,否则返回false

public static boolean isPrimeNumber(int num){

int k = (int) Math.sqrt(num);

if(num == 2)

return true;

for(int i=2; i<=k; i++)

if(num%i == 0)

return false;

return true;

}

}

流程图

实验任务]递归方法

1、 实验要求:

1)必须用递归函数实现上述问题;

2)注意编程规范:程序开头部分的目的,作者以及日期;必要的空格与缩进,适当的注释等;

3)实验报告中要求包括程序设计思想、程序流程图、源代码、运行结果截图、编译错误分析等内容。

2、   实验内容

(1) 使用递归方式判断某个字串是否是回文( palindrome );

“回文”是指正着读、反着读都一样的句子。比如“我是谁是我”

使用递归算法检测回文的算法描述如下:

A single or zero-character string is a palindrome.

Any other string is a palindrome if the first and last characters are the same, and the string that remains, excepting those characters, is a palindrome.

程序设计思想

输入一个字符串,然后将字符串倒置,比较字符串第i位上的字符与倒数第i位上的字符是否相同,如果都相同则字符串是回文;否则字符串不是回文。

package LIULAN;

import java.util.Scanner;

public class SS {

    public static boolean isPalindrome(String s,int i,int j){  

        if(i > j)  

            throw new IllegalArgumentException();  

        if(i == j)  

            return true;  

        else{  

            return (s.charAt(i) == s.charAt(j)) && isPalindrome(s,i+1,j-1);  

        }  

    }  

       

    public static void main(String[] args){  

        Scanner in=new Scanner(System.in);

        String s = in.nextLine();

        int i = 0;

        int j = s.length() - 1;  

        System.out.println(s + " is Palindrome? " + SS .isPalindrome(s, i, j));  

    }   

}

[实验任务三]统计分析。

1、 实验要求:

实验报告中要求包括程序设计思想、程序流程图、源代码、运行结果截图、编译错误分析等内容。

2、实验内容:

(1) 用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《哈利波特》 中最常用的短语是什么,等等。

(2) 要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

1 实验思想

1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。

2)定义一个Mapgetkey是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。

3)遍历(1)中得到的字符串数组,对于每一个单词,考察Mapgetkey中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value1(第一次出现);

如果,在mapgetkey中发现了该单词,则通过key找到对应的value(单词出现的次数),将该value1,再次保存回map

4)遍历(3)中得到的map,输出getkey(单词)及对应的value(次数)。

 

 

package LIULAN;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.Map.Entry;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class en {

public static void Count(File file){

String str ="";

String result = "";

try {BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

while((str = bufferedReader.readLine())!=null){result = result+str;}bufferedReader.close();

}

catch (Exception e) {

}

System.out.println(result);

Map<String, Integer> map = new HashMap<String, Integer>();

Pattern p = Pattern.compile("[, . ; ! ? ]");

Matcher m = p.matcher(result);

String [] strs = p.split(result);

for(int i=0;i<strs.length;i++)

{ if(map.containsKey(strs[i])){int c = map.get(strs[i]);

c++;map.put(strs[i], c);

                      }else{map.put(strs[i], 1);

}

}

Set set = map.entrySet();

Iterator it = set.iterator();

int min  = 100;

int max = 0;

String minWord = "";

String maxWord = "";

int x = 0;

while (it.hasNext()) {Entry<String, Integer> me = (Entry) it.next();

if((int) me.getValue()<min&&!((String) me.getKey()).equals("")){min = (int) me.getValue();

minWord = (String) me.getKey();

}

if((int) me.getValue()>=max&&!((String) me.getKey()).equals(""))

    {

max = (int) me.getValue();

maxWord = (String) me.getKey();

}

System.out.println(me.getKey()+":"+me.getValue());

                     }System.out.println("出现次数最多的是"+":"+max+"   "+maxWord);

                          }

private void println(Map map){Set set = map.entrySet();

      Iterator it = set.iterator();

while(it.hasNext()){Entry<String, Integer> entry = (Entry<String, Integer>) it.next();

String key = entry.getKey();

int value = entry.getValue();

}

  }

public static void main(String[] args){

File file = new File("D:\\新建文件夹\\1\\KANWEN\\A.txt");

    Count(file);}

}

 

动手动脑

重载的动手动脑

      参数类型不同,参数个数不同,或者是参数类型的顺序不同。

7和7.5的类型不同造成了重载

import java.util.Random;

import java.util.Scanner;

public class RandomNum {

    

    public static void main(String[] args) {

        

        Random ran = new Random(System.currentTimeMillis());//以当前时间为种子

        

        Scanner input = new Scanner(System.in);

        

        System.out.print("Enter the number of randomnumbers:");//从键盘输入要产生随机数的个数

        

        int in = input.nextInt();

        

        int j=0;//引入j用来输出换行符

        

        for(int i = 0 ; i < in ; i++)

            

        {

            

            System.out.print(ran.nextInt()+"\t");//利用for循环输出所产生的随机数

            

            j+=1;

            

            if(j==6)

                

            {

                

                System.out.println();

                

                j=0;

                

            }

            

        }

        

    }

}

猜你喜欢

转载自www.cnblogs.com/yang-qiu/p/9787070.html