Java if else语句中的逻辑错误 [英] Logical Error in if else statement in java

查看:123
本文介绍了Java if else语句中的逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

import org.ujmp.core.Matrix;
import org.ujmp.core.SparseMatrix;

public class part {
    public static void main(String args[]) throws Exception{
        Matrix Bigomega=Matrix.Factory.zeros(6,6);
        Matrix omega = SparseMatrix.Factory.zeros(6, 6);
        int []timea={1,2,3,4,5,6};
        int [] timeb={3};
        int k1=0,k2=0;
        while (k1 < timea.length && k2 < timeb.length ) {
            if (timea[k1] < timeb[k2]) {
                omega.setAsInt(1, k1, k1);
                omega.setAsInt(-1, k1, k1 + 1);
                omega.setAsInt(-1, k1 + 1, k1);
                omega.setAsInt(1, k1 + 1, k1 + 1);
                Bigomega = Bigomega.plus(omega);
                omega.clear();
                k1++;
            }
            else if (timea[k1] == timeb[k2]){
                omega.setAsInt(1, k1, k1);
                omega.setAsInt(-1, k1, k1 + 1);
                omega.setAsInt(-1,k1+1,k1);
                omega.setAsInt(1,k1+1,k1+1);
                Bigomega=Bigomega.plus(omega);
                omega.clear();
                k2++;
            }
        }
        System.out.println(Bigomega);
    }
}

输出:

1.0000    -1.0000     0.0000     0.0000     0.0000     0.0000
-1.0000     2.0000    -1.0000     0.0000     0.0000     0.0000
0.0000    -1.0000     2.0000    -1.0000     0.0000     0.0000
0.0000     0.0000    -1.0000     1.0000     0.0000     0.0000
0.0000     0.0000     0.0000     0.0000     0.0000     0.0000
0.0000     0.0000     0.0000     0.0000     0.0000     0.0000

不需要的输出.获取timea[k1] == timeb[k2]后,循环在此处终止.但是我想在从timea和timeb获得相似的元素之后,循环继续进行,直到timea的所有元素都访问为止.根据我的代码,它从timea数组访问1,2,3,然后将其丢弃.由于while语句while (k1 < timea.length && k2 < timeb.length ),这非常明显.因为当它获得循环计数器k2++时k2会增加1.因此从timeb中获得3之后,k2就会变为1.因此当它获得k2<timeb.length时意味着1<1这是错误的,因此循环终止.但是在那个时候,timea的{4,5,6}没有被访问.我想在执行else语句后,如果{4,5,6}语句再次执行.

The output that came that is not desired. The loop was terminated here after getting the timea[k1] == timeb[k2]. But I want after getting the similar element from timea and timeb the loop continue until all the elements of timea visit. As per my code it visits 1,2,3 from timea array and then discards. And this is very obvious because of while statement while (k1 < timea.length && k2 < timeb.length ). Because when it get loop counter k2++ k2 increment by 1. So after getting 3 from timeb k2 become 1.So when it get k2<timeb.length that means 1<1 which is false so the loop is terminated. But in that time {4,5,6} of timea is not visited. I want after executing else statement if statement executes again for {4,5,6}.

如何实现?

所需的输出:

1.0000    -1.0000     0.0000     0.0000     0.0000     0.0000
-1.0000     2.0000    -1.0000     0.0000     0.0000     0.0000
0.0000    -1.0000     2.0000    -1.0000     0.0000     0.0000
0.0000     0.0000    -1.0000     2.0000     -1.0000     0.0000
0.0000     0.0000     0.0000     -1.0000     2.0000     -1.0000
0.0000     0.0000     0.0000     0.0000     1.0000     -1.0000

推荐答案

在您的代码中,在您的if条件下,如果它们==则执行某些操作,而在timea [k1]<时执行某些操作. timeb [k2],但如果timea [k1]> timeb [k2],则您什么也不做.这就是为什么您会错过一些迭代的原因.您可以处理这种情况,也可以使else> =而不只是==.

In your code is that in your if condition you do something if they are == and you do something if timea[k1] < timeb[k2] but you don't do anything if timea[k1] > timeb[k2]. That's why you miss some iterations. You can handle that case or make the else >= instead of just ==.

我要指出的另一件事是,Java中接受了一些编码约定,您最好使用它们来获得可读的代码.例如,BigOmega应该称为小写的bigOmega.

Another thing I would like to point out is that there are some coding conventions that are accepted in Java and you better use them to have a readable code. For example BigOmega should be called bigOmega with a lowercase.

最后一件事是您的while循环.您可以像这样做一样,使其比重复代码更具可读性.

And a last thing is your while loop. You can do it like that which will make it more readable than repeating code.

 while (k1 < timea.length && k2 < timeb.length ) {
        omega.setAsInt(1, k1, k1);
        omega.setAsInt(-1, k1, k1 + 1);
        omega.setAsInt(-1, k1 + 1, k1);
        omega.setAsInt(1, k1 + 1, k1 + 1);
        Bigomega = Bigomega.plus(omega);
        omega.clear();
    if (timea[k1] < timeb[k2])
        k1++;
     else          
        k2++;        
}

这篇关于Java if else语句中的逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆