软件构造作业
生成算式存入csv
package kousuanti; import java.util.Scanner; public class Gongneng { public static void main(String[] args) { Scanner scan=new Scanner(System.in); Chuti chuti = new Chuti(); System.out.println("请确定生成几道题:"); int n1=scan.nextInt(); chuti.setShuLiang(n1); System.out.println("请确定几个数相计算:"); int n2=scan.nextInt(); chuti.setXiangJia(n2); System.out.println("请确定数值范围(输入最大值):"); int n3=scan.nextInt(); chuti.setFanWei(n3); System.out.println("请确定是否有乘除法(1为是,0为否):"); int n4=scan.nextInt(); if(n4==1) chuti.setChengChu(true); else chuti.setChengChu(false); System.out.println("是否保存此套题(1为是,0为否):"); int n5=scan.nextInt(); if(n5==1) { chuti.Show(); SaveCsv saveCsv = new SaveCsv(); saveCsv.save(chuti.ChangeToSave()); } else chuti.Show(); } }
package kousuanti; import java.util.Random; public class Chuti { private int ShuLiang=50;//出题数量 private int HuangHang=5;//几题换行 private int XiangJia=3;//几个数相加 private int FanWei=100;//出题范围 private boolean isChengChu=false;//是否有乘除法 private int[][] JiLu;//出题记录 private int i_JiLi=0; private void ShengCheng() { JiLu=new int[ShuLiang][XiangJia*2-1]; int is; if(isChengChu) is = 4; else is = 2; for (int i = 0; i < ShuLiang; i++) { int arr[]=MyRandom(XiangJia*2-1,FanWei); for (int j = 0; j < XiangJia-1; j++) { arr[j*2+1]=arr[j*2+1]%is; } if(isNotRepeat(arr)) { if(XiangJia==2) { if(isFuHeYaoQiu(arr)){ JiLu[i_JiLi] = arr; i_JiLi++; }else i--; }else { JiLu[i_JiLi] = arr; i_JiLi++; } } else i--; } } public void Show() { ShengCheng(); for (int i = 0; i < ShuLiang; i++) { if(i%HuangHang==0&&i!=0) System.out.println(); for (int j = 0; j < XiangJia-1; j++) { System.out.print(JiLu[i][j*2]); switch(JiLu[i][j*2+1]) { case 0:System.out.print("+");break; case 1:System.out.print("-");break; case 2:System.out.print("*");break; case 3:System.out.print("/");break; } } System.out.print(JiLu[i][XiangJia*2-2]+"= "); } } public String[] ChangeToSave(){ String[] strings = new String[ShuLiang]; int t=0; for (int i = 0; i < ShuLiang; i++) { String str = ""; for (int j = 0; j < XiangJia-1; j++) { str = str + JiLu[i][j*2]; switch(JiLu[i][j*2+1]) { case 0:str = str + "+";break; case 1:str = str + "+";break; case 2:str = str + "*";break; case 3:str = str + "/";break; } } str = str + JiLu[i][XiangJia*2-2]+"="; strings[t]=str; t++; } return strings; } private boolean isNotRepeat(int arr[]) { if(i_JiLi==0) return true; int m=i_JiLi; int n=XiangJia*2-1; for(int j=0;j<m;j++) for(int i=0;i<n;i++) if(JiLu[j][i]!=arr[i]) return true; return false; } private boolean isFuHeYaoQiu(int arr[]){ if(arr[1]==0){ if(arr[0]+arr[2]>FanWei) return false; else return true; } else { if(arr[0]-arr[2]<0) return false; else return true; } } //随机,num数量,max范围 private int[] MyRandom(int num,int max) { Random r = new Random(); int arr[]=new int[num]; for(int i=0;i<num;i++) arr[i]=r.nextInt(max); return arr; } public int getShuLiang() { return ShuLiang; } public void setShuLiang(int shuLiang) { ShuLiang = shuLiang; } public int getHuangHang() { return HuangHang; } public void setHuangHang(int huangHang) { HuangHang = huangHang; } public int getXiangJia() { return XiangJia; } public void setXiangJia(int xiangJia) { XiangJia = xiangJia; } public int getFanWei() { return FanWei; } public void setFanWei(int fanWei) { FanWei = fanWei; } public boolean isChengChu() { return isChengChu; } public void setChengChu(boolean chengChu) { isChengChu = chengChu; } public int[][] getJiLu() { return JiLu; } public void setJiLu(int[][] jiLu) { JiLu = jiLu; } }
package kousuanti; import java.io.*; public class SaveCsv { public void save(String strings[]){ String filePath="D:\\Idea\\CSV\\"; File file = new File(filePath + "KouSuanTi_01.csv"); try { //如果存在,则删除,新建文件 if (file.exists()) { file.delete(); } file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream out=null; OutputStreamWriter osw=null; BufferedWriter bw=null; try { out = new FileOutputStream(file); osw = new OutputStreamWriter(out,"UTF-8"); bw =new BufferedWriter(osw); //写入数据 for(int i=0;i< strings.length;i++) { bw.append(strings[i]); bw.append("\r"); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(bw!=null) bw.close(); bw=null; if(osw!=null) osw.close(); osw=null; if(out!=null) out.close(); out=null; } catch (IOException e) { e.printStackTrace(); } } } }
标签:总结,23.11,arr,int,每日,System,XiangJia,str,out From: https://www.cnblogs.com/laobing6214/p/17804662.html