I made a little random math generator, which gives out 2 random numbers and a random operator but when I call my check result method in my ok button, I always get the "Errado" ("Wrong") string which means it's not properly comparing the value of the textfield with the result of whatever math problem was presented. Leaving relevent code from both classes down here with comments on where I'm having issues and 2 for ease of understanding. Help's appreciated!
public class geraAritmetica {
ArrayList<Integer> nums = new ArrayList();
int a;
char ops = '?';
int max = 10;
int min = 1;
int range = max - min + 1;
int res;
Random r = new Random();
public int gerarNums(){
for(int i = 0; i<10; i++){
a = (int) (Math.random()*range) + min;
nums.add(a);
}
return a;
}
public char gerarOps(){
switch(r.nextInt(4)){
case 1 : ops = '+';
res = nums.get(0) + nums.get(1);
break;
case 2: ops = '-';
res = nums.get(0) - nums.get(1);
break;
case 3: ops = '*';
res = nums.get(0) * nums.get(1);
break;
case 4: ops = '/';
res = nums.get(0) / nums.get(1);
break;
default: ops = '+';
res = nums.get(0) + nums.get(1);
break;
}
return ops;
}
public boolean checkRes(){ //everytime I call this method always get the same string "Errado" ("Wrong") in the label.
if(Integer.parseInt(FrameEnsinoAritmetica.jTextField1.getText()) == res){
FrameEnsinoAritmetica.jLabel2.setText("Correto!");
return true;
}
else FrameEnsinoAritmetica.jLabel2.setText("Errado!");
return false;
}
}
public class FrameEnsinoAritmetica extends javax.swing.JFrame {
geraAritmetica a = new geraAritmetica(); //creating object of class at the start of my JFrame class
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { //generating numbers and operator (it prints fine in another label)
int b = a.gerarNums();
int c = a.gerarNums();
char s = a.gerarOps();
jLabel1.setText(b + " " +s+ " " + c);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
a.checkRes(); //everytime I call this method always get the same string "Errado" ("Wrong") in the label.
}