Problem with the IF in java

-1

classmates I was starting with a new class and I was surprised how the if it does not work with whole numbers that is to say it does not compare correctly the greater than and the smaller that The code is as follows

import java.util.Scanner;

public class Conservacion {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int c1,c2,c3,i1,i2,i3,vi,vc,total;
        Scanner lee=new Scanner(System.in);
        while(lee.hasNext()) {
            total=0;
            vc=0;
            vi=0;
            c1=lee.nextInt();
            c2=lee.nextInt();
            c3=lee.nextInt();
            i1=lee.nextInt();
            i2=lee.nextInt();
            i3=lee.nextInt();
            vc=(c1*c2*c3);
            vi=(i1*i2*i3);

            if(vc>vi) {
                total=vc/vi;
                System.out.println(total);
            }
            if(vi<vc) {
                System.out.println("0");
            }

        }
    }

And this is the exit

10 10 10 9 9 11

1

0

IF YOU NOTIFY ME BOTH IF IF IT IS SAY IT DOES NOT WORK CORRECTLY THE FIRST IF IF YOU KNOW HOW TO SOLVE IT, THANK YOU THANK YOU

    
asked by jason argonauta 02.09.2018 в 02:41
source

2 answers

1

There is no problem in the if, the problem is with the condition. If you look closely, you are reversing the order of the variables and at the same time you use the reverse operator ... that is basically the same condition.

vc>vi is the same as vi<vc

    
answered by 02.09.2018 в 02:48
0

// VC greater than VI ...

if(vc>vi) {   
    total=vc/vi;
    System.out.println(total);
}

// VI less than VC (same as in previous if only in reverse)

if(vi<vc) {
    System.out.println("0");
}

/////////////////////////////////////////////// //////// // MY opinion ..

if( VC > VI ) {   
    total=vc/vi;
    System.out.println(total);
}else if( VC < VI ) {
    System.out.println("0");
}

// in this way or is an if or is the other

si( vc MAYOR_que vi){código...}  (SI o NO)
entonces si(vc MENOR_que vi ){código..}

I hope to be of help in your little confusion, it is only when making the comparison ...

    
answered by 06.09.2018 в 04:08