-1

The code below is supposed to randomly create two integer numbers which will then be displayed to a user who then needs to divide the two numbers; however, when I try to change the answer or response to both be doubles it doesn't work. If you do run it as is and get a question such as 1/5 then the answer ends up being 0 since it's an integer; same with 5/4 which ends up as 1.

//Variables
String name;
String probType;
String longProbType;
int numProb;
int loFactor;
int hiFactor;
int factor1;
int factor2;
int answer;
int response;
int score;
double scorePct;

case "D":
    for(int i = 0; i < numProb; i++) {
        factor1 = random.nextInt(hiFactor - loFactor + 1) + loFactor;
        factor2 = random.nextInt(hiFactor - loFactor + 1) + loFactor;
        if(factor2 == 0) {
            factor2 += 1;
        }
        System.out.print(String.format("\n%d / %d = ", factor1, factor2));
        response = input.nextInt();
        input.nextLine();
        
        answer = factor1 / factor2;
        
        if(answer == response) {
            score += 1;
            System.out.println("Correct!");
            history[i] = String.format("%d / %d = %d, Correct, correct answer is %d", factor1, factor2, response, answer);
        } else {
            System.out.println(String.format("Incorrect! Correct answer is %d", answer));
            history[i] = String.format("%d / %d = %d, Incorrect, correct answer is %d", factor1, factor2, response, answer);
        }
    }
    
    System.out.println("\nSession Summary");
    System.out.println(String.format("%d problems, %d correct", numProb, score));
    
    scorePct = ((double)score / numProb) * 100;
    System.out.println(String.format("Score is  %.1f\n", scorePct));
    
    System.out.println("\nProblems");
    for(int i = 0; i < numProb; i++) {
        System.out.println(history[i]);
    }
    
    System.out.println(outromessage);
    break;
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Probably relevant: [Comparing double values for equality in Java](https://stackoverflow.com/questions/25160375/comparing-double-values-for-equality-in-java) – Hovercraft Full Of Eels Jun 13 '22 at 14:38
  • Well, `answer` is an int, so `1.25` wouldn't make sense. Integer division works differently than floating-point, and one option (in addition to making it `double answer`) would be to cast one of your integers before performing the division. `factor2` can only be `0` if `loFactor` is 0, in which case you'd be right to exclude it for a rational number. – Rogue Jun 13 '22 at 14:39
  • For you division to work properly you need to have `answer`, `response` and one of your factors as `double`. Otherwise the division will be made as `int` and round up/down. If one of the factors is `double` it will properly calculate a decimal number. – RatzzFatzz Jun 13 '22 at 14:40

1 Answers1

-1

For the division, you need to cast one of the int values to a double. The JRE will then do floating point division and produce a double. This result will also need to be captured in a double.

double result = factor1 / (double) factor2;

Note that testing floating point equality can be tricky and produce false negatives/positives.

vsfDawg
  • 1,425
  • 1
  • 9
  • 12