java - Using a random operator many variables -


I want to add a random operators so that I can randomly add / subtract / multiply / divide multiple variables. The code below does not work, I think this is because randomly generated operator is a four which is actually a value, so it has been added instead of being used as an operator. Anyway I'm new on this and any help would be great thanks!

  class squares {public static zero main (string [] args) {classb x = new classb (); Int a = 1, b = 2, c = 3, d = 4; Integer answer; // Trying to get answer / Answer = A (+, -, *, /) B (+, -, *, /) C (+, -, *, /) D / y = answer + a + X.randomOperator () + B + x.randomOperator () + c + x.randomOperator () + D; Println ("answer =" + answer); }}  

I create random operator sign in a different class.

  import java.util.Random; Public Class Classby (Public Care Random Operator) (Random RV = New Random); Int apple = RV.NextText (3); Four Operators; Switch (Apple) {Case 1: Operator = ';'; Breakdown; Case 2 : Operator = '-'; break; case 3: operator = '*'; break; default: operator = '/'; breakage;} return operator;}}  

I also think Is that I should add that I am trying to do it for a number of numbers, while the two numbers are fine, I add it And it becomes very complicated so I can not really write out every combination probably this can not be done only, but I thought I would ask! Thank you!

You can not do that which you call '+' is just a four and that means the value There is no other meaning except

  interface operation {int eval (int a, int b);} class edition {int equal (int a, int b) {returns a + b;}} bridegroom C subtraction {int eval (int a, a back int b) {- b;}} ... Operation [] operations = new operation [] {new (in addition), a new subtraction (), ..}; Int executeRandom (int a, int b) {return action [random.nextInt (operations.length)]. Eval (a, b); }  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -