fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. String[] operators = {"-", "+", "*"};
  12. String operator;
  13. Random r = new Random();
  14. int result = -1;
  15.  
  16. for (int j = 0; j < 10; j++) {
  17. for (int i = 0; i < 3; i++) {
  18. int randNum = r.nextInt(9) + 1;
  19. if (i != 0) {
  20. operator = operators[r.nextInt(operators.length)];
  21. System.out.print(operator);
  22. result = calculate(result, randNum, operator);
  23. }
  24. else {
  25. result = randNum;
  26. }
  27. System.out.print(randNum);
  28. }
  29. System.out.println("=" + result);
  30. }
  31. }
  32. public static int calculate(int operand1, int operand2, String operator) {
  33. switch (operator) {
  34. case "+":
  35. return operand1 + operand2;
  36. case "-":
  37. return operand1 - operand2;
  38. case "*":
  39. return operand1 * operand2;
  40. default:
  41. throw new RuntimeException();
  42. }
  43. }
  44. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
7-8-7=-8
7+6*2=26
3*2*4=24
1-1+1=1
1*9+9=18
4+7*3=33
8*4-2=30
5*8+7=47
6+1+1=8
7*2-5=9