简体   繁体   中英

Loop is turning my negative integer positive in java

This code in java seems to be turning my negative integers positive. I'm passing in a -9 and 1 and in my debugger, the i in my first for loop is starting at 27 instead of negative 27. What's even weirder is that if I add a line to make i = -1, it toggles between -27 and -28 forever. Can someone tell me why?

I'm passing in a -9, 1. The result should be a number divisible by 3 and a modulus of 5. I think -29 meets this criteria.

public static int getIntegerH(int x, int y) {
    System.out.println("The result of the getIntegerH method:");
    boolean flag = true;

    int n = x * 4;  // 36
    int m = x * 3;  // 27

    if (x == y) {
      flag = true;
    }

    if (x < 0 || y < 0) {
        for (int i = -m; i < -n; i++) {
             if (i / 3 == x && i % 5 == y ) {
                  System.out.println(i);
                  flag = false;
              }
        }
    } else if (x > 0 || y > 0) {
        for (int i = m; i < n; i++) {
            if (i / 3 == x && i % 5 == y ) {
                System.out.println(i);
                flag = false;
            }
        }
    }

    if (flag == true) {
        System.out.println("No such number");
    }

    return 0;
}

You set i to the negative of m.

  for (int i = -m; i < -n; i++) {

If you passed -9 for x, then m is -27, and the negative of -27 is 27.

As a side note, you'll also want to look at your condition and the increment. If you want to count down from -27 to -36, you'll want something like this:

  for (int i = m; i >= n; --i ) {

Here's a solution for you that can work with whichever divisor or modulo you like:

final static int DIV = 3;
final static int MOD = 5;

/** Returns an integer `r` such that `r / DIV = x` and `r % MOD = y`, else returns 0 */
public static int getIntegerH(int x, int y) {
    if (y >= MOD) {
        System.out.println(
                "getIntegerH(" + x + ", " + y + ") -> none; impossible to have a remainder of " + y
                        + " when the modulo is " + MOD);
        return 0;
    }

    // determine the min and max numbers inclusive where n / DIV = x
    int min = x > 0 ? x * DIV : x * DIV - DIV + 1;
    int max = x > 0 ? x * DIV + DIV - 1 : x * DIV;

    // get the min and max values of the mod for this
    int minMod = min % MOD;
    int maxMod = max % MOD;

    if ((MOD > DIV) && (y < minMod || y > maxMod)) {
        System.out
                .println("getIntegerH(" + x + ", " + y + ") -> none; there is no number `n` between "
                        + min + " and " + max + " where `n % " + MOD + " = " + y + "`");
        return 0;
    }

    int r = (x > 0) ? min + y - minMod : max + y - maxMod;
    System.out.println("getIntegerH(" + x + ", " + y + ") -> " + r + "; " + r + " / " + DIV + " = ["
            + (r / DIV) + "]; " + r + " % " + MOD + " = [" + (r % MOD) + "]");
    return r;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM