lundi 20 août 2018

Logical operators in 'else if' statement

I am trying to learn "if else" statements and I am having trouble with the middle 'if else' part of the script.

package practice;
import java.util.Scanner;
public class Practice {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);
    System.out.println("enter a number between 1 and 10 ");
    if (!in.hasNextDouble()) {
        String word = in.next();
        System.err.println(word + " is not a number");
    } else if (!(in.nextDouble() > 0) || !(in.nextDouble() <= 10)) {
        Double wrongnumber = in.nextDouble();
        System.err.println(wrongnumber + " is not between 1 and 10");
    } else {
        System.out.println("It works!");
    }
       return;

   }

 }

There are no errors but in the 'else if' block I can't get it to print the err "..... not between 1 and 10", whether or not I put a number between 1 and 10 or higher. It also wont print the "it works!" line anymore when I add the 'else if' block. any suggestions would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire