日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA第二次验证设计性实验报告 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

[實驗任務(wù)]素數(shù)輸出

3)實驗報告中要求包括程序設(shè)計思想、程序流程圖、源代碼、運行結(jié)果截圖、編譯錯誤分析等內(nèi)容。

1、???實驗內(nèi)容

1)計算并輸出3~100之間的素數(shù)。

2)編程滿足下列要求:

?1)按照每行5個輸出;

?2)輸出任意兩個整數(shù)之間的所有素數(shù);

?3)輸入兩個整數(shù),輸出這兩個整數(shù)之間的最大的10個和最小的10個素數(shù)。

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("請輸入起始的數(shù)字:");

????????int?n=input.nextInt();

????????System.out.println("請輸入結(jié)束的數(shù)字:");

????????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("最小的十個素數(shù):");

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

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

????????}

????????System.out.println();

????????System.out.println("最大的十個素數(shù):");

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

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

????????}

}

}

3、?設(shè)計思路

利用兩次循環(huán);第一個為所求素數(shù)范圍的循環(huán),這個范圍由用戶輸入;第二個循環(huán)來判斷是否為素數(shù),若是素數(shù),則存到數(shù)組里;判斷素數(shù)時,就有順序,所以存到數(shù)組中的素數(shù)也是有是順序的,就可以直接利用數(shù)組輸出最大和最小的十個素數(shù)。

4、?實驗截圖

[實驗任務(wù)]遞歸方法

1、???實驗內(nèi)容

使用遞歸方式判斷某個字串是否是回文( 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方法去比較第一個和最后一個字符,如果一樣,前一個后移一位,后一個前移一位,再次比較,如此下去,直到前一個等于后一個,在主方法中調(diào)用這個方法。

4、?實驗截圖:

?[實驗任務(wù)三]統(tǒng)計分析。

1、?實驗內(nèi)容:

用戶需求:英語的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()//創(chuàng)建文本

?

?{

????????

????????String path= "c:\\文章\\統(tǒng)計";//所創(chuàng)建文件的路徑

????????

????????File f = new File(path);

????????

???????if(!f.exists()){

????????????

????????????f.mkdirs();//創(chuàng)建目錄

????????}

????????

????????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:\\文章\\統(tǒng)計\\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:\\文章\\統(tǒng)計" + File.separator + "abc.txt") ; ???// 聲明File對象

?????????// 第2步、通過子類實例化父類對象

?????????Reader input = null ; ???// 準備好一個輸入的對象

?????????try {

input = new FileReader(f) ?;

} catch (FileNotFoundException e) {

// TODO 自動生成的 catch 塊

e.printStackTrace();

} ???// 通過對象多態(tài)性,進行實例化

?????????// 第3步、進行讀操作

?????????char c[] = new char[1024] ; ???????// 所有的內(nèi)容都讀到此數(shù)組之中

?????????try {

int len = input.read(c) ;

s=String.valueOf(c);

???} catch (IOException e) {

// TODO 自動生成的 catch 塊

e.printStackTrace();

} ???????// 讀取內(nèi)容

????????// 第4步、關(guān)閉輸出流

?????????try {

input.close() ;

} catch (IOException e) {

// TODO 自動生成的 catch 塊

e.printStackTrace();

?

????????}

?????????

??????

return s;}

?public void tongjufile(String s)//統(tǒng)計文本

?{

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];//創(chuàng)建類將單詞和個數(shù)聯(lián)系起來

?

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);

}

}

轉(zhuǎn)載于:https://www.cnblogs.com/fuheishi/p/9790382.html

總結(jié)

以上是生活随笔為你收集整理的JAVA第二次验证设计性实验报告的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。