java之重构四则运算
Posted masuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之重构四则运算相关的知识,希望对你有一定的参考价值。
今天又来一次课堂测试,这次测试让我对文件流的了解又加深了好多好多,这次就像升华一般,虽然和其他人比还有区别,但与以前的我大不一样了。
1 import java.io.*; 2 import java.util.*; 3 4 import org.omg.Messaging.SyncScopeHelper; 5 public class Creat { 6 int wrongNum=0; 7 static File file=new File("answ.txt"); 8 static FileWriter fw=null; 9 static FileReader fr = null; 10 int[] answ = new int[31]; 11 String[] timu = new String[31]; 12 //题目创建并存储 13 public void creat() throws IOException { 14 15 for (int i = 0; i < 3; i++) { 16 17 int choice = (int) (Math.random() * 5); 18 switch (choice) { 19 // + 20 case 0: 21 doAdd(i + 1); 22 break; 23 // - 24 case 1: 25 doReduce(i + 1); 26 break; 27 // * 28 case 2: 29 doMul(i + 1); 30 break; 31 // / 32 case 3: 33 doDivide(i + 1); 34 break; 35 } 36 } 37 } 38 // / 39 private void doDivide(int i) throws IOException { 40 // TODO Auto-generated method stub 41 System.out.print("第" + i + "次为:"); 42 43 int one = (int) (Math.random() * 10); 44 int two = (int) (Math.random() * 10); 45 while (two == 0) { 46 two = (int) (Math.random() * 10); 47 } 48 answ[i] = one / two; 49 write(one,two,"/"); 50 System.out.println("答案为" + answ[i]); 51 System.out.println(one + "/" + two + "=" + " "); 52 } 53 54 // * 55 private void doMul(int i) throws IOException { 56 // TODO Auto-generated method stub 57 System.out.print("第" + i + "次为:"); 58 int one = (int) (Math.random() * 10); 59 int two = (int) (Math.random() * 10); 60 answ[i] = one * two; 61 String numOne = String.valueOf(one); 62 String numTwo = String.valueOf(two); 63 timu[i] = numOne + numTwo; 64 write(one,two,"*"); 65 System.out.println("答案为" + answ[i]); 66 System.out.println(one + "*" + two + "=" + " "); 67 } 68 // - 69 private void doReduce(int i) throws IOException { 70 // TODO Auto-generated method stub 71 System.out.print("第" + i + "次为:"); 72 int one = (int) (Math.random() * 10); 73 int two = (int) (Math.random() * 10); 74 answ[i] = one - two; 75 String numOne = String.valueOf(one); 76 String numTwo = String.valueOf(two); 77 timu[i] = numOne + numTwo; 78 write(one,two,"-"); 79 System.out.println("答案为" + answ[i]); 80 System.out.println(one + "-" + two + "=" + " "); 81 } 82 // + 83 private void doAdd(int i) throws IOException { 84 // TODO Auto-generated method stub 85 System.out.print("第" + i + "次为:"); 86 int one = (int) (Math.random() * 10); 87 int two = (int) (Math.random() * 10); 88 answ[i] = one + two; 89 String numOne = String.valueOf(one); 90 String numTwo = String.valueOf(two); 91 timu[i] = numOne + numTwo; 92 write(one,two,"+"); 93 System.out.println("答案为" + answ[i]); 94 System.out.println(one + "+" + two + "=" + " "); 95 } 96 //存储写入 97 private void write(int one,int two,String f ) throws IOException { 98 // TODO Auto-generated method stub 99 fw=new FileWriter(file,true); 100 BufferedWriter bw=new BufferedWriter(fw); 101 bw.write(one+f+two+"="); 102 bw.newLine();// 表示换行 103 bw.write("*"); 104 bw.newLine();// 表示换行 105 bw.flush();// 清空缓冲区 106 bw.close();// 关闭输出流 107 } 108 //测试判断区 109 public void judge() throws IOException { 110 //首先,读文件 111 Scanner sc=new Scanner(System.in); 112 BufferedReader br = null; 113 fr = new FileReader(file); 114 br = new BufferedReader(fr); 115 String s = null; 116 int i=0; 117 while ((s = br.readLine())!=null) { 118 119 if (s.equals("*")) { 120 System.out.println("请输入答案:"); 121 @SuppressWarnings("unused") 122 int a = sc.nextInt(); 123 if(a==answ[i]) { 124 125 } 126 else 127 wrongNum++; 128 } else { 129 System.out.println(s); 130 } 131 132 } 133 fr.close(); 134 System.out.println("一共错了"+wrongNum+"个!"); 135 } 136 }
import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Test t=new Test(); t.comeTrue(); } private void comeTrue() throws IOException { // TODO Auto-generated method stub Creat c=new Creat(); c.creat(); c.judge(); } }
以上是关于java之重构四则运算的主要内容,如果未能解决你的问题,请参考以下文章