标签:java except scanner main string stringbu print int() mkdir
/* * 信1705-2 谢培龙 20173611 */ package 自主出题; import java.io.File; import java.io.FileOutputStream; import java.util.Random; import java.util.Scanner; public class Test{ public static String s3; public void writeToFile(String data){//写入 byte[] sourceByte = data.getBytes(); String path = "D:"; String fileName = "test.txt"; if(null != sourceByte){ try { File file = new File(path+fileName);//文件路径(路径+文件名) if (!file.exists()) { //文件不存在则创建文件,先创建目录 File dir = new File(file.getParent()); dir.mkdirs(); file.createNewFile(); } FileOutputStream outStream = new FileOutputStream(file); //文件输出流将数据写入文件 outStream.write(sourceByte); outStream.close(); } catch (Exception e) { e.printStackTrace(); // do something } finally { // do something } } } public static void main(String[] args){ int grade=0; Test l=new Test(); System.out.println("输入题数:"); Scanner in=new Scanner(System.in); int a1=in.nextInt(); StringBuffer f = new StringBuffer(); for(int i=0;i<a1;i++) { Random a=new Random(); int q=a.nextInt(100); Random b=new Random(); int w=b.nextInt(100); char c = 0; int sum=0; int x=(int)Math.floor(Math.random()*4+1); if(x==1) { c=‘+‘ ;sum=q+w; } if(x==2) { c=‘-‘ ; sum=q-w;} if(x==3) { c=‘*‘ ; sum=q*w;} if(x==4) { c=‘/‘; sum=q/w; } System.out.println(q); System.out.println(c); System.out.println(w); System.out.println("="); Scanner result=new Scanner(System.in); int r=result.nextInt(); if(r==sum) { System.out.println("恭喜答对了! "); grade=grade+1;} else System.out.println("很遗憾 答错了! "); String s=String.valueOf(q); String s1=String.valueOf(c); String s2=String.valueOf(w); String s0=String.valueOf(sum); String s5=s+s1+s2+"="+s0+"*"+"\t"; s3 =s5+s3; } System.out.println("答对了"+grade+"道题"); l. writeToFile(s3); } }
四则运算测试题
一道一道题输出判断对错
并将题目及正确答案输出到文本文档
标签:java except scanner main string stringbu print int() mkdir
原文地址:https://www.cnblogs.com/1983185414xpl/p/9964159.html