Why are the conditions not met?

2

When I start this program, for example I put value 11, I do not get into the conditional if ((valor >10)&&(valor <20)) , why can it be?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author carlos
 */
import java.util.Scanner;
public class UsoCondiciones {
    public static void main (String[]args){

    Scanner Scan = new Scanner (System.in);

    int valor =0;
   valor = Scan.nextInt();

    if((valor >0)&&(valor<10)){

    Scan.nextLine();
    System.out.println("Valor del 1 al 9");
    if ((valor >10)&&(valor <20)){
              System.out.println("Valor del 10 al 19");


 if ((valor >= 20)&&(valor<30)){
              System.out.println("Valor del 20 al 29");

  }
    }else{
    System.out.println("Incorrcete");


}
}
    }
}
    
asked by Carlos 08.10.2016 в 08:29
source

1 answer

2

because you have the nested if, then if you do not enter the first if you skip the others:

Scanner Scan = new Scanner (System.in);

    int valor =0;
   valor = Scan.nextInt();

    if((valor >0)&&(valor<10)){

    Scan.nextLine();
    System.out.println("Valor del 1 al 9");
    if ((valor >10)&&(valor <20)){
              System.out.println("Valor del 10 al 19");
if ((valor >= 20)&&(valor<30)){ System.out.println("Valor del 20 al 29");

} }else{ System.out.println("Incorrcete");

} } } }

To work for you, it should be something like this:

if(valor>10 $$ valor<10){
   Scan.nextLine();
    System.out.println("Valor del 1 al 9");
}else if(valor>=10 && valor<20){
    System.out.println("Valor del 10 al 19");
}else{
    System.out.println("Incorrcete");
}

Here after each check look at the other, never leave any if without checking. :)

    
answered by 08.10.2016 / 08:53
source