Java 对于不变量,以下循环是否正确?

Java 对于不变量,以下循环是否正确?,java,loops,Java,Loops,可能重复: 对于不变量,以下for循环是否正确? 不变量:b[x]是b[h…s-1]的最小值 int x = h; int h = s-1; // {inv: b[x] is the minimum of b[h...s-1]} while (s-1 != k) { s = s-1; if (b [s-1] < b[x]) { x = s-1;} } intx=h;int h=s-1; //{inv:

可能重复:

对于不变量,以下for循环是否正确? 不变量:b[x]是b[h…s-1]的最小值

 int x = h;      int h = s-1;

    // {inv: b[x] is the minimum of b[h...s-1]}
    while (s-1 != k) {
       s = s-1;
       if (b [s-1] < b[x])
          { x = s-1;}
    }
intx=h;int h=s-1;
//{inv:b[x]是b[h…s-1]的最小值}
while(s-1!=k){
s=s-1;
if(b[s-1]
如果要查找数组的最小值,可以这样做

int b[] = {1,3,2,5,2,3};

//min needs to have a starting value so b[0] works fine.
int min = b[0];

//This loops over the remaining elements in the array. If it finds a value smaller than the current minimum, it reassigns min to that value.
for(int i = 1; i < b.length; i++)
{
     if(b[i] < min)
          min = b[i];
}

//If you haven't covered for loops yet, here is how you can do it with a while.
int i = 1;
while(i < b.length)
{
     if(b[i] < min)
          min = b[i];
     i++;
}

//b.length is just a way of getting the length of an array.
intb[]={1,3,2,5,2,3};
//min需要有一个起始值,以便b[0]可以正常工作。
int min=b[0];
//这将在数组中的其余元素上循环。如果它发现一个小于当前最小值的值,它会将最小值重新指定给该值。
for(int i=1;i
在将代码发布到此处之前,您至少应该尝试编译代码!你能重新措辞你的问题吗?请尝试重写代码,因为
s-1=h
在Java中是无效的表达式。作业只能从右向左进行,即
h=s-1
就可以了。对不起,我是Java新手,我还不知道怎么做。我只是在纸上写的。这也是为什么Java不应该被用于教学的另一个原因(不容易访问[good]REPL)…它应该是像
intminimum=b[h];如果(b[i]<最小值)最小值=b[i]