Java 用无限循环解析代码

Java 用无限循环解析代码,java,Java,我有一个optimumRequestOrder的代码。它有三种主要方法:request(int len,int Qty)、request(int len)和calculate()。 不幸的是,当尝试诸如(Integer.Max\u值、Integer.Max\u值)之类的大数字时,计算代码将进入无限循环 导入java.io.PrintStream; 公共类OptimumRequestOrder{ 私有int xstdLen; 私有int-maxRequest; 私有整数len=0; 私有int[]

我有一个optimumRequestOrder的代码。它有三种主要方法:request(int len,int Qty)、request(int len)和calculate()。 不幸的是,当尝试诸如(Integer.Max\u值、Integer.Max\u值)之类的大数字时,计算代码将进入无限循环

导入java.io.PrintStream;
公共类OptimumRequestOrder{
私有int xstdLen;
私有int-maxRequest;
私有整数len=0;
私有int[][]数据请求;
公共OptimumRequestOrder(整数大小){
this.maxRequest=大小;
这是0.len=0;
this.dataRequest=newint[this.maxRequest][this.maxRequest];
}
公共无效设置长度(内部标准){
this.xstdLen=stdLen;
}
公共作废请求(整数长度、整数数量){
如果(!full()){
this.dataRequest[this.len][0]=inLength;
this.dataRequest[this.len][1]=inQty;
这是1.len+=1;
}否则{
System.err.println(“溢出\n”);
}
}
公共无效delRequest(内部长度){
内部温度=0;
如果(!empty()){
for(int i=0;i”+dataR[0][0]+“”+dataR[0][1]);
while(wloop1(dataR)){
System.out.println(“wloop1=“+i+”:“+dataR[i][0]”数量:“+dataR[i][1]);
如果(i=dataR[i][0]){
系统输出打印项次(“数量”+i+:“+dataR[i][1]);
如果(数据r[i][1]>0){
结余-=dataR[i][0];
数据r[i][1]-=1;
}否则{
系统输出打印项次(“数量=0”);
i+=1;
总数n+=1;
}
}否则{
System.out.println(“平衡库朗”);
i+=1;
}
}else if(WLOOP00(数据、余额)){
System.out.println(“balance masih boleh ditolak”);
if(wloop1(dataR)){
系统输出打印项次(“平衡量为0”);
i=0;
}否则{
系统输出打印项次(“余额masih boleh ditolak n数量=0”);
总回报率;
}
}否则{
系统输出打印项次(“平衡数据”);
if(wloop1(dataR)){
系统输出打印项次(“余额tiada n qty0”);
i=0;
balance=this.xstdLen;
总数n+=1;
System.out.println(“totalen==>>>>”+totalen);
}否则{
系统输出打印项次(“余额tiada n数量=0”);
总回报率;
}
}
}
总回报率;
}
系统错误打印项次(“下溢”);
返回0;
}
公共布尔wloop0(int[][]数据,int-bal){
for(int i=0;i=dataR[i][0])&&(dataR[i][1]>0)){
返回true;
}
}
返回false;
}
公共布尔值wloop1(int[][]dataR){
for(int i=0;i0){
返回true;
}
}
返回false;
}
公共布尔空(){
返回this.len==0;
}
公共布尔值full(){
返回this.len==this.maxRequest;
}
public void printq(){
系统输出打印(“len:+this.len”);
for(int i=0;i

请帮助我解决此问题。

您的问题是您从未更改此while条件:while(wloop1(dataR))。您需要做的是将其设置为false,以离开while循环并返回值。我建议您首先将
wloop1
方法重命名为更有意义的方法。这样,每个人(包括你)都更容易理解它应该做什么/返回什么。类似于
的内容包含positiveOnSecondRow()
或类似内容。对于
wloop0
同样,您的问题是,您永远不会更改while条件:while(wloop1(dataR))。您需要做的是将其设置为false,以离开while循环并返回值。我建议您首先将
wloop1
方法重命名为更有意义的方法。这样,每个人(包括你)都更容易理解它应该做什么/返回什么。类似于
的内容包含positiveOnSecondRow()
或类似内容。对于
wloop0
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();
 }
}