1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Random; 4 import java.util.Scanner; 5 import java.util.function.BiFunction; 6 public class FourArithmetic { 7 private List<Subject> subjectList; 8 public List<Subject> getSubjectList() { 9 return subjectList; 10 } 11 public void setSubjectList(List<Subject> subjectList) { 12 this.subjectList = subjectList; 13 } 14 public FourArithmetic() { 15 System.out.print("输入题目数量\n"); 16 Scanner count = new Scanner(System.in); 17 int c = count.nextInt(); 18 List<Subject> list = new ArrayList<Subject>(); 19 BiFunction<Integer, Integer, Integer> sum = (x, y) -> x + y; 20 BiFunction<Integer, Integer, Integer> minus = (x, y) -> x - y; 21 BiFunction<Integer, Integer, Integer> multiple = (x, y) -> x * y; 22 BiFunction<Integer, Integer, Integer> divide = (x, y) -> x / y; 23 for (int i = 0; i < c; i++) { //题目数量 24 Subject subject = new Subject(); 25 Integer a = new Random().nextInt(99); 26 Integer b = new Random().nextInt(99); 27 Float a1 = new Random().nextFloat(); 28 Float b1 = new Random().nextFloat(); 29 subject.setA(a); 30 subject.setB(b); 31 subject.setA1(a1); 32 subject.setB1(b1); 33 subject.setIndex(i + 1); 34 Integer symbol = new Random().nextInt(4);//控制加减乘除出现 35 if (symbol == 0) { 36 subject.setSymbol("+"); 37 subject.setAnswer(sum.apply(a, b)); 38 } else if (symbol == 1) { 39 subject.setSymbol("-"); 40 subject.setAnswer(minus.apply(a, b)); 41 } else if (symbol == 2) { 42 subject.setSymbol("×"); 43 subject.setAnswer(multiple.apply(a, b)); 44 } else if (symbol == 3) { 45 subject.setSymbol("÷"); 46 subject.setAnswer(divide.apply(a, b)); 47 } 48 list.add(subject); 49 } 50 this.subjectList = list; 51 } 52 53 public static void main(String[] args) { 54 FourArithmetic f = new FourArithmetic(); 55 System.out.println("四则运算:"); 56 f.getSubjectList().forEach(subject -> { 57 System.out.println("(" + subject.getIndex() + ") " + subject.getA() + subject.getSymbol()+subject.getB() + "= ?"); 58 }); 59 System.out.print("*******答案*******\n"); 60 f.getSubjectList().forEach(subject -> { 61 System.out.println("第" + subject.getIndex() + "题:" + subject.getAnswer()); 62 }); 63 } 64 } 65 class Subject { 66 private Integer index; 67 private Integer a; 68 private Integer b; 69 private String symbol; 70 private Integer answer; 71 private Float a1; 72 private Float b1; 73 public Integer getIndex() { 74 return index; 75 } 76 public void setIndex(Integer index) { 77 this.index = index; 78 } 79 public Integer getA() { 80 return a; 81 } 82 public void setA(Integer a) { 83 this.a = a; 84 } 85 public Integer getB() { 86 return b; 87 } 88 public void setB(Integer b) { 89 this.b = b; 90 } 91 public void setB1(Float b1) { 92 this.b1 = b1; 93 } 94 public Float getB1() { 95 return b1; 96 } 97 public void setA1(Float a1) { 98 this.a1 = a1; 99 } 100 public Float getA1() { 101 return a1; 102 } 103 public String getSymbol() { 104 return symbol; 105 } 106 public void setSymbol(String symbol) { 107 this.symbol = symbol; 108 } 109 public Integer getAnswer() { 110 return answer; 111 } 112 public void setAnswer(Integer answer) { 113 this.answer = answer; 114 } 115 }
原文:https://www.cnblogs.com/Cute-pig/p/15345935.html