解决具有无限循环的代码

时间:2017-02-15 19:00:37

标签: java

我有一个OptimalRequestOrder的代码。它有3个主要方法:request(int len,int Qty),request(int len)和calculate()。 不幸的是,在尝试使用(Integer.Max_value,Integer.Max_value)这样的大数字时,计算代码会进行无限循环。

import java.io.PrintStream;

public class OptimumRequestOrder {
 private int xstdLen;
 private int maxRequest;
 private int len = 0;
 private int[][] dataRequest;

 public OptimumRequestOrder(int size) {
    this.maxRequest = size;
    this.len = 0;
    this.dataRequest = new int[this.maxRequest][this.maxRequest];
 }

 public void setStdLen(int stdLen) {
    this.xstdLen = stdLen;
 }

 public void request(int inLength, int inQty) {
    if (!full()) {
        this.dataRequest[this.len][0] = inLength;
        this.dataRequest[this.len][1] = inQty;
        this.len += 1;
    } else {
        System.err.println("Overflow\n");
    }
 }

 public void delRequest(int inLength) {
    int temp = 0;
    if (!empty()) {
        for (int i = 0; i < this.len; i++) {
            if (this.dataRequest[i][0] == inLength) {
                temp = i;
            }
        }
        for (int i = temp; i < this.len; i++) {
            this.dataRequest[i][0] = this.dataRequest[(i + 1)][0];
            this.dataRequest[i][1] = this.dataRequest[(i + 1)][1];
        }
        len = len - 1;
    } else {
        System.err.println("Underflow");
    }
 }

 public int[][] getDataRequest() {
    return this.dataRequest;
 }

 public int getLen() {
    return this.len;
 }

 public int calculate() {
    if (!empty()) {
        int balance = this.xstdLen;
        int totalLen = 1;
        int i = 0;
        int[][] dataR = this.dataRequest;
        System.out.println("ni dataR===>" + dataR[0][0] + " " + dataR[0][1]);
        while (wloop1(dataR)) {
            System.out.println("wloop1=" + i + " : " + dataR[i][0] + " qty: " + dataR[i][1]);
            if (i < this.len) {
                System.out.println("balance=" + balance);
                if (balance >= dataR[i][0]) {
                    System.out.println("QTY " + i + ": " + dataR[i][1]);
                    if (dataR[i][1] > 0) {
                        balance -= dataR[i][0];
                        dataR[i][1] -= 1;
                    } else {
                        System.out.println("Qty=0");
                        i += 1;
                        totalLen += 1;
                    }
                } else {
                    System.out.println("balance kurang");
                    i += 1;
                }
            } else if (wloop0(dataR, balance)) {
                System.out.println("balance masih boleh ditolak");
                if (wloop1(dataR)) {
                    System.out.println("balance masih boleh ditolak n qty<>0");
                    i = 0;
                } else {
                    System.out.println("balance masih boleh ditolak n qty=0");
                    return totalLen;
                }
            } else {
                System.out.println("balance dah tak boleh ditolak");
                if (wloop1(dataR)) {
                    System.out.println("balance tiada n qty<>0");
                    i = 0;
                    balance = this.xstdLen;
                    totalLen += 1;
                    System.out.println("totalLen==>>>>" + totalLen);
                } else {
                    System.out.println("balance tiada n qty=0");
                    return totalLen;
                }
            }
        }
        return totalLen;
    }
    System.err.println("Underflow");
    return 0;
 }

 public boolean wloop0(int[][] dataR, int bal) {
    for (int i = 0; i < this.len; i++) {
        if ((bal >= dataR[i][0]) && (dataR[i][1] > 0)) {
            return true;
        }
    }
    return false;
 }

 public boolean wloop1(int[][] dataR) {
    for (int i = 0; i < this.len; i++) {
        if (dataR[i][1] > 0) {
            return true;
        }
    }
    return false;
 }

 public boolean empty() {
    return this.len == 0;
 }

 public boolean full() {
    return this.len == this.maxRequest;
 }

 public void printq() {
    System.out.print("len: " + this.len);
    for (int i = 0; i < this.len; i++) {
        System.out.print("dataR[" + i + ",0]=" + this.dataRequest[i][0] + "; ");
        System.out.print("dataR[" + i + ",1]=" + this.dataRequest[i][1] + "; ");
    }
    System.out.println();
 }
}

请帮我解决这个问题。

0 个答案:

没有答案