Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最佳算法:当我找不到与我要替换的数字匹配的数字时,如何停止if语句_Java_Eclipse - Fatal编程技术网

Java 最佳算法:当我找不到与我要替换的数字匹配的数字时,如何停止if语句

Java 最佳算法:当我找不到与我要替换的数字匹配的数字时,如何停止if语句,java,eclipse,Java,Eclipse,我正在做FIFO LRU和Optimal。我有一个优化的问题,用一个我们在最长时间内不会使用的替换它,所以最远的一个。我用它来代替我的fram号码,而我在最长的时间里不使用它。但问题是,当我想要替换的数字根本不存在时,我该如何阻止我的IF政治家?我不知道该停止哪一个?我试过这个: public int toss( int pr ) { // if we get the same number just return -1 for (int s=numberOfFrames-1; s>=

我正在做FIFO LRU和Optimal。我有一个优化的问题,用一个我们在最长时间内不会使用的替换它,所以最远的一个。我用它来代替我的fram号码,而我在最长的时间里不使用它。但问题是,当我想要替换的数字根本不存在时,我该如何阻止我的IF政治家?我不知道该停止哪一个?我试过这个:

public int toss( int pr ) 
{
// if we get the same number just return -1

for (int s=numberOfFrames-1; s>=0; s--) 

{
    // if we get the same number dont kick anything 
    if ( pr == fram[s]) 

    {

        return -1;
    }
}

// find which frame to replace

int look2 = 0; // this is the co for the number you want to replace with.
// next time it is used.
int co = 0; // this is index to pra.
int r = -1;

   for ( int d=numberOfFrames-1; d>=0; d--) 
   {
       lookFor(d,r);



   }


   if (r == -1) 

   {
       //item not found, handle however you want, one suggestion is:
       return -1;  //have the caller handle this correctly
   }
   else

   {
       int q = fram[r]; // remember 2 which is page we are getting raid off
       fram[r] = pra[co]; //  fram one we want to get raid off and replace it with the                       pr
       return q; // return the one we kicked
   }
}

int co=0; int-look2=0

公共int lookForint d,int r {

}
}

如果您在System.exit0所在的位置中断循环,您将只查看pra[co],因为f=co,而不是使用pra[f]循环。您可能希望将中断放在if中,因此当您找到匹配项时,您将停止此循环。但这将使您从内部for循环中退出,您可能希望完全停止循环。有关断开多个循环的详细信息,请参见

int r = -1;
for ( int d=numberOfFrames-1; d>=0; d--) {
    for( int f=co; f>=0; f++) {
        if( fram[d] == pra[f]) {
            if(look2<=f) {
                r = d;
                look2 = f;
                break;
            }
        }
    }
}
if (r == -1) {
    //item not found, handle however you want, one suggestion is:
    return -1;  //have the caller handle this correctly
} else {
    int q = fram[r]; // remember 2 which is page we are getting raid off
    fram[r] = pra[co]; //  fram one we want to get raid off and replace it with the pr
    return q; // return the one we kicked
}

请注意,如果您打算进一步讨论java,请阅读。将此规则应用于代码将使代码更短、更清晰。System.exit0;将终止JVM。这就是你想要的吗?休息;很可能是您想要用-1停止循环初始化器,然后在循环结束后检查它是否仍然是-1,如果是这样,您还没有找到您要查找的值。@Thomas,我想终止我的if语句,所以当我没有得到数字时,我希望它停止?好的,我把它放在哪里,现在它一直在返回-1?@user2989685-1是一个特殊的值,表示没有找到任何东西,所以最后你需要检查你是否得到-1,并采取相应的行动。检查我是否得到-1的一种方法是什么?当我休息的时候;在外面它给了我-1和9?在外面我的意思是在循环之外看到我的编辑,你可能会想先设置r=-1,然后在循环之后处理结果。请记住,当前中断仅退出内部循环,外部循环仍在运行直至完成。
int r = -1;
for ( int d=numberOfFrames-1; d>=0; d--) {
    for( int f=co; f>=0; f++) {
        if( fram[d] == pra[f]) {
            if(look2<=f) {
                r = d;
                look2 = f;
                break;
            }
        }
    }
}
if (r == -1) {
    //item not found, handle however you want, one suggestion is:
    return -1;  //have the caller handle this correctly
} else {
    int q = fram[r]; // remember 2 which is page we are getting raid off
    fram[r] = pra[co]; //  fram one we want to get raid off and replace it with the pr
    return q; // return the one we kicked
}