-4

I tried to develop the function in one way, but after multiple failed tests I created another way, but it keeps not generating what I'm searching for. I'm focusing only on 'Easy Division'.

The outputs appeared are like 7/2; 9/9; 9/8; 5/2;, and what I want is divisions like 9/9; 9/1; 6/3; 6/2;.

With remainder equaling 0 divisions. I hope you can help me, and sorry for my rusty English.

void generateDivisionOperation(String dificult){
    int n1, n2, aux;
    if(dificult == "Easy"){
        n1 = generateEasyNumber();
        do{
            aux = generateEasyNumber();
            if(remainderCalculatedBetween(n1,aux) != 0)
                aux = generateEasyNumber();
        }while (remainderCalculatedBetween(n1,aux) != 0);
                n2 = aux;
                textViewOperacaoN1.setText(String.valueOf(n1));
                textViewOperacaoN2.setText(String.valueOf(n2));
     }else{
        n1 = generateHardNumber();
        do{
            n2 = generateHardNumber();
        }while(n2 > n1 && n1%n2!=0);
            textViewOperationN1.setText(String.valueOf(n1));
            textViewOperationN2.setText(String.valueOf(n2));
    }
}

int generateEasyNumber(){
    int n1;
    int rangeEN = (9 - 1) + 1;
    n1 = (int) (Math.random() * rangeEN) + 1;
    return n1;
}

int generateHardNumber(){
    int n2;
    int rangeHN = (99 - 10) + 1;
    n2 = (int) (Math.random() * rangeHN) + 1;
    return n2;
}

int remainderCalculatedBetween(int n1, int n2){
    int remainder;
    remainder = n1 - n2*(int)(n1/n2);
    return remainder;

}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
volkel
  • 1
  • 1

4 Answers4

2

Use a modulo sign % e.g. 10 % 3 is 1

Mike
  • 1,313
  • 12
  • 21
0

java provides '%' (modulus) operator to find the remainder. so the 'if' statement can be rewritten as:

if((n1%aux) != 0)

Sneha
  • 39
  • 6
  • Already used that but it keeps appearing a division like 7/4. Thanks anyways. – volkel Feb 02 '17 at 17:58
  • if(dificult == "Easy"){ do{ n1 = generateEasyNumber(); aux = generateEasyNumber(); }while ((n1%aux) != 0); n2 = aux; textViewOperacaoN1.setText(String.valueOf(n1)); textViewOperacaoN2.setText(String.valueOf(n2)); } changing the code as above would help? – Sneha Feb 02 '17 at 18:45
  • i know the code you write make sense, but it seems the problem is not about the statement, i already tried all the answers here, the problem is not from those statements i guess – volkel Feb 02 '17 at 20:55
0
if(dificult.equals("Easy")){
    n1 = generateEasyNumber();
    do {
      aux = generateEasyNumber();
    } while (n1 % aux != 0);
    n2 = aux;
    textViewOperacaoN1.setText(String.valueOf(n1));
    textViewOperacaoN2.setText(String.valueOf(n2));
}

This works. I've tried it myself. Remove: if(remainderCalculatedBetween(n1,aux) != 0) aux = generateEasyNumber();

Replace == "Easy" with .equals("Easy")

and replace remainderCalculatedBetween(n1, aux) with n1 % aux;

I this does not work for you, perhaps you're not passing "Easy" to the function.

Mehmet K
  • 2,805
  • 1
  • 23
  • 35
  • Ok @howdoidothis thanks for the effort, tried your answer now, this still not showing what i look for. The glitch should be anything else and not this function. – volkel Feb 03 '17 at 18:30
  • But this IS the part you asked about. You said you're only focusing on "Easy" division, and this is the only part of code related to that. And my answer solves the 2 problems you have with that part of the code. If you havent debugged your code to know that the error generates from this function why are you asking for our help at that specific point. – Mehmet K Feb 06 '17 at 12:01
0

Well the problem was in another point of the program, as @Mehmet Kologlu said that version is correct. Sorry for answering late. Already running without problems.

volkel
  • 1
  • 1