JAVA第二次验证设计性实验报告

[实验任务]素数输出

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

1、   实验内容

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

2)编程满足下列要求:

 1)按照每行5个输出;

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

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

2、 源代码

import java.util.Scanner;

public class PrimeNumber {

public static void main(String[] args) {

        int count=1;

        int b=0;

        int[] a=new int[100];

        Scanner input=new Scanner(System.in);

        System.out.println("请输入起始的数字:");

        int n=input.nextInt();

        System.out.println("请输入结束的数字:");

        int m=input.nextInt();

        input.close();

        for(int i=n;i<=m;i++){

            int j;

            for(j=2;j<i;j++){

                if(i%j==0){

                    break;

                }

            }

            if(j==i){

                a[b]=i;

                b++;

                if(count%5==0){

                    System.out.print(i+" ");

                    System.out.println();

                }else{

                    System.out.print(i+" ");

                }

                count++;

            }

        }

        System.out.println();

        System.out.println("最小的十个素数:");

        for(int c=0;c<10&&c<count;c++) {

            System.out.print(a[c]+" ");

        }

        System.out.println();

        System.out.println("最大的十个素数:");

        for(int c=count-2;c>count-12;c--) {

            System.out.print(a[c]+" ");

        }

}

}

3、 设计思路

利用两次循环;第一个为所求素数范围的循环,这个范围由用户输入;第二个循环来判断是否为素数,若是素数,则存到数组里;判断素数时,就有顺序,所以存到数组中的素数也是有是顺序的,就可以直接利用数组输出最大和最小的十个素数。

4、 实验截图

[实验任务]递归方法

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.

2、 源代码:

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

    System.out.println("请输入需要判断的任意一个字符串:");

    Scanner input=new Scanner(System.in);

    String str=input.nextLine();

    input.close();

    int n=0;

    int m=str.length()-1;

    if(palin(str,n,m))

    System.out.println("这个字符串是回文字符串");

    else

    System.out.println("这个字符串不是回文字符串");

    }

    public static boolean palin(String str,int n,int m){

        if(n > m)

            throw new IllegalArgumentException();

        if(n == m)

            return true;

        else{

            return (str.charAt(n) == str.charAt(m)) && palin(str,n+1,m-1);

        }

    }

}

3、 实验思路

先定义一个判断回文的方法,先得到字符串的长度,利用charAt方法去比较第一个和最后一个字符,如果一样,前一个后移一位,后一个前移一位,再次比较,如此下去,直到前一个等于后一个,在主方法中调用这个方法。

4、 实验截图:

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

1、 实验内容:

用户需求:英语的26 个字母的频率在一本小说中是如何分布的?

 

2、 源代码:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.util.Scanner;

 

public class Statistics {

 public void createFile()//创建文本

 

 {

        

        String path= "c:\\文章\\统计";//所创建文件的路径

        

        File f = new File(path);

        

       if(!f.exists()){

            

            f.mkdirs();//创建目录

        }

        

        String fileName = "abc.txt";//文件名及类型

        

        File file = new File(path, fileName);

        

        if(!file.exists()){

            

            try {

                file.createNewFile();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            

        }

       

    }            

 public void shuruFile()//输入文本

   {

 Scanner in=new Scanner(System.in);

 try {

FileWriter fw = new FileWriter("c:\\文章\\统计\\abc.txt");

String world;

world=in.nextLine();

fw.write(world);

   fw.close();

   } catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

 }

  

   }

public String duqufile()//读取文本

{String s = null;

File f= new File("c:\\文章\\统计" + File.separator + "abc.txt") ;    // 声明File对象

         // 第2步、通过子类实例化父类对象

         Reader input = null ;    // 准备好一个输入的对象

         try {

input = new FileReader(f)  ;

} catch (FileNotFoundException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}    // 通过对象多态性,进行实例化

         // 第3步、进行读操作

         char c[] = new char[1024] ;        // 所有的内容都读到此数组之中

         try {

int len = input.read(c) ;

s=String.valueOf(c);

   } catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}        // 读取内容

        // 第4步、关闭输出流

         try {

input.close() ;

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

 

        }

         

      

return s;}

 public void tongjufile(String s)//统计文本

 {

String[] a=new String [10000];

int[] b=new int [1000];

int n=0;//一共的字母

 int k=0;//单词

char c[] = s.toCharArray();

 for(;c[n]!='\0'; n++)//将文本中单词存入a[]中

 {

 int j=0;

 for(j=n;c[j]!=' ';j++)

 {

 

 }

a[k]=s.substring(n,j);b[k]=1;n=j;

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

 if(a[i].equals(a[k]))

 {b[i]++;k--;break;}

k++;

 

}

 k--;

 word[] z=new word[k];//创建类将单词和个数联系起来

 

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

{

z[i].num=b[i];

z[i].world=a[i];

}

word t = null,m = null;

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

{

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

{

if(z[j].num<z[i].num)

{

m.deng(t,z[j]);

m.deng(z[j],z[i]);

m.deng(z[i],t);

}

}

}

 

System.out.println(z[0].num+"  "+z[0].world);

 

 

 }

public static void main(String[] args) {

// TODO 自动生成的方法存根

Statistics a=new Statistics();

a.shuruFile();

String c;

c=a.duqufile();

a.tongjufile(c);

}

}

猜你喜欢

转载自www.cnblogs.com/fuheishi/p/9790382.html
今日推荐