MSG INCORRECT DATA

0

Can you tell me in this code because when I put a result between 1 and 10, it throws me to the% wrong% data%?

    public static void main(String[] args) {

        Scanner Scan = new Scanner(System.in);

        //variables
        int data, dia, any, cinturo, genere, mes;
        int edat;
        boolean tipusCorrecte;
        boolean pesReal;
        double pes;

        edat = 0;
        genere = 0;
        cinturo = 0;
        pes = 0;
        any = 0;
        mes = 0;
        dia = 0;
        int id = 0;

        System.out.println("PRUEBA");
        System.out.println("INTRODUCE ID:");

        tipusCorrecte = Scan.hasNextInt();
        if (tipusCorrecte) {

            id = Scan.nextInt();
            Scan.nextLine();

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

                if ((edat > 4) && (edat < 17)) {
                System.out.println("Introdueix edat:");
                edat = Scan.nextInt();
                Scan.nextLine();
                 } else {
                System.out.println("dato incorrecte");
                }

            } else {
                System.out.println("dato incorrecte");

            }


        } else {
            System.out.println("dato incorrecte");
        }
    }
}
    
asked by Carlos 08.10.2016 в 16:57
source

1 answer

0
  

You have the same String ("incorrect data") for all your else   (It is not good or a good practice. You must have personalized your   messages)

So if you do not%% of your program you will have doubts where the printing of such a message occurs. As in this case

At first glance the message occurs in your second debug nested (within another if )

if ((edat > 4) && (edat < 17)) {...}

When you enter this if your variable if has the initialization value that is 0, therefore you will have

 /* False && True*/
if ((0> 4) && (0< 17)) {...}
  

Logic Operators Logic this will result edat so   which enters your False and prints the message.    link

    
answered by 08.10.2016 / 19:12
source