當前位置:編程學習大全網 - 編程軟體 - JAVA 輸入 輸出 流 編程 設計 求幫助

JAVA 輸入 輸出 流 編程 設計 求幫助

這個程序是用來篩選1-100中的素數,在寫入文件的同時,在控制臺輸出。

package 文件操作;

import java.io.FileOutputStream;

import java.io.IOException;

public class ss {

ss()

{

try

{

FileOutputStream f=new FileOutputStream("1.txt");

int i,j,k;

for(i=1;i<100;i++)

{

boolean flag=true;

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

{

if(i%j==0)

{

flag=false;

break;

}

}

if(flag)

{

f.write(i);

}

}

f.flush();

f.close();

}

catch(IOException e)

{

e.printStackTrace();

}

}

public static void main(String [] args){

new ss();

}

}

//sr.java文件

package 文件操作;

import java.io.FileInputStream;

import java.io.IOException;

public class sr {

sr(){

try{

FileInputStream w=new FileInputStream("1.txt");

int l=0,c;

while((c=w.read())!=-1){

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

l++;

}

System.out.print("1-100的素數***有:"+l);

w.close();

}catch(IOException w)

{

w.printStackTrace();

}

}

public static void main(String[] args) {

new sr();

}

}

運行結果:

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 1-100的素數***有:26

  • 上一篇:可與iPad遊戲軟件互動的迪士尼玩具車
  • 下一篇:C語言中%d是什麽意思?
  • copyright 2024編程學習大全網