Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. private int rando(int i) {
  2. Random randomGenerator = new Random();
  3. int randomInt = randomGenerator.nextInt();
  4. i = randomInt;
  5. return rando();
  6. }
  7.  
  8. public static int add(int i, int i)
  9. {
  10. int a = i+i;
  11. return a;
  12. }
  13.  
  14. b1.addActionListener(new ActionListener() {
  15. public void actionPerformed(ActionEvent e){
  16. String b = "Click For Answer";
  17. b1.setText(b.toString());
  18.  
  19. jl.setText(prob.toString());
  20. }
  21. });
  22.  
  23. public class MathProblems {
  24. private static final int MAX_NUMBER = 10;
  25. private static final Scanner in = new Scanner(System.in);
  26. private static final Random random = new Random();
  27.  
  28. public static void main (String... args) {
  29. new MathProblems().run();
  30. }
  31.  
  32. public void run() {
  33. while(true) {
  34. final int a = random.nextInt(MAX_NUMBER);
  35. final int b = random.nextInt(MAX_NUMBER);
  36.  
  37. final int type = random.nextInt(4);
  38.  
  39. switch (type) {
  40. case 0:
  41. add(a, b);
  42. break;
  43. case 1:
  44. subtract(a, b);
  45. break;
  46. case 2:
  47. multiply(a, b);
  48. break;
  49. }
  50. }
  51. }
  52.  
  53. private void add(final int a, final int b) {
  54. final int expected = a + b;
  55.  
  56. final int answer = askQuestion(a + " + " + b + "=");
  57.  
  58. checkResult(expected, answer);
  59. }
  60.  
  61. private void subtract(final int a, final int b) {
  62. final int expected = a - b;
  63.  
  64. final int answer = askQuestion(a + " - " + b + "=");
  65.  
  66. checkResult(expected, answer);
  67. }
  68.  
  69. private void multiply(final int a, final int b) {
  70. //leaving this for you..
  71. }
  72.  
  73. private int askQuestion(final String question) {
  74. System.out.print(question);
  75.  
  76. return in.nextInt();
  77. }
  78.  
  79. private void checkResult(final int expected, final int answer) {
  80. if (expected == answer) {
  81. System.out.println("Correct answer! You rock!");
  82. } else {
  83. System.out.println("WROOONG! You suck!");
  84. }
  85. }
  86. }
  87.  
  88. //or whatever numbers you want here
  89. int myLow = -10;
  90. int myHigh = 10;
  91.  
  92. public int randBetween(int low, int high){
  93. return (int) Math.round(Math.random() * (high - low)) + low;
  94. }
  95.  
  96. public String makeProblem(AtomicLong answer){
  97. int a = randBetween(myLow, myHigh);
  98. int b = randBetween(myLow, myHigh);
  99. double result;
  100. char operator;
  101. switch(randBetween(1,4)){
  102. case 1:
  103. operator = '+';
  104. result = a + b;
  105. break;
  106. case 2:
  107. operator = '-';
  108. result = a - b;
  109. break;
  110. case 3:
  111. operator = '*';
  112. result = a * b;
  113. break;
  114. case 4:
  115. operator = '/';
  116. result = (double) a / b;
  117. break;
  118. default:
  119. operator = '';
  120. result = Double.NaN;
  121. }
  122. answer.set(Double.doubleToLongBits(result));
  123. return String.format("%d %c %d = ?",a,operator,b);
  124. }
  125.  
  126. public void yourFunction(){
  127.  
  128. //whatever you need to do before
  129.  
  130. AtomicLong outVariable = new AtomicLong();
  131. String question = makeProblem(outVariable);
  132. double answer = Double.longBitsToDouble(outVariable.get());
  133.  
  134. //whatever you need to do after
  135.  
  136. }
  137.  
  138. public class MyClass{
  139.  
  140. Random random;
  141.  
  142. public MyClass(){
  143. random = new Random();
  144. }
  145.  
  146.  
  147. private String randomNumber(){
  148. return Integer.toString(random.nextInt());
  149. }
  150.  
  151. private String randomOperator(){
  152. String[] operators = {"+", "/", "-", "*"};
  153. int i = random.nextInt(4);
  154. return operators[i];
  155. }
  156.  
  157. public void execute(){
  158. String sum = randomNumber() + randomOperator() + randomNumber();
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement