java(九九乘法表输出1,3,5,7,9)

来源:互联网 发布:mac 打开安卓模拟器 编辑:程序博客网 时间:2024/06/04 19:26

1*1=1

1*3=3 2*3=6 3*3=9

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

代码如下:
package jiujiu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Jiu {
public void Dayin(String filedy){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
if(i%2==1){
int ji=j*i;
String str1=j+”*”+i+”=”+ji+” “;
writefile(str1,filedy);
}else{
}
}
String str2=”\r\n”;
writefile(str2,filedy);

        }}private void writefile(String data,String filedy) {    // TODO Auto-generated method stub    File file=new File(filedy);    if(!file.exists()){        try {            file.createNewFile();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    OutputStream os=null;    try {        os=new FileOutputStream(file,true);        byte[]bytes=data.getBytes();        os.write(bytes);        os.flush();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }finally {        try {            if(os!=null){                os.close();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}public static void main(String[] args){    Jiu jiu=new Jiu();    jiu.Dayin("f:/test/444.txt");}

}
执行结果:
这里写图片描述

0 0
原创粉丝点击