Monday, 15 September 2014

java - Is it possible to increase array index in a loop? -



java - Is it possible to increase array index in a loop? -

so problem if want new calculation, operaotr in array should change. how can that? im trying hours :/ how far got. first starts + , whan take new calculation next 1 should - , on.... need switch "operator[0]"

private static void mathgame() { char[] operator = {'+', '-', '*', '/'}; int x; int y = 0; scanner input = new scanner(system.in); { int operand1 = (int) (math.random() * (100 - 1) + 1); int operand2 = (int) (math.random() * (100 - 1) + 1); system.out.println("solve"); system.out.println(operand1 + " " + operator[0] + " " + operand2); x = input.nextint(); // operator [1]++; if (x == evaluate(operand1, operator[0], operand2)) { system.out.println("true!"); system.out.println("new challange?"); y = input.nextint(); //operator[1]++; } else if (x != evaluate(operand1, operator[0], operand2)) { system.out.println("false!"); system.out.println("new challange?"); y = input.nextint(); //operator[1]++; } } while (y == 1); }

you need define variable outside of loop (int = 0;) , have instruction within loop (maybe before "} while (y == 1);":

i = (i + 1) % 4; (4 size of operators array).

p.s. think need give more details question , elaborate question in order create more clear future readers.

java arrays

No comments:

Post a Comment