-3

The code I say has to complete these assignments: Given an integer,N, perform the following conditional actions:

If N is odd, print Weird If N is even and in the inclusive range of 2 to 5 , print Not Weird If N is even and in the inclusive range of 6 to 20 , print Weird If N is even and greater than 20 , print Not Weird Complete the stub code provided in your editor to print whether or not N is weird.

My code looked like this:

private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        scanner.close();
        int Numberparorimpa = N % 2;
        if(N < 2 || Numberparorimpa ==1 || N <=20 &&  N >=6 ){
            System.out.println("Weird");
        }else{
            if(N >=2 && Numberparorimpa == 0){
                 System.out.println("Not Weird");
            }else{
             if(Numberparorimpa == 0 && N >=6 || N<=20){
                  System.out.println("Weird");
             }else{
                 if(Numberparorimpa== 0 && N> 20){
                     System.out.println("Not Weird");
                 }else{
                     return;
                 }
             }   
            }
        }
            }
        
    }

How can I reduce the IFs of this code?

4
  • 1
    In Java naming conventions, a variable’s name begins with a lowercase letter. An all-uppercase name means a constant. Commented Jan 9, 2023 at 8:34
  • You can try to reduce your number of if else by merging some conditions together using && and || Commented Jan 9, 2023 at 8:41
  • You should think through the requirements before writing code. There is only one case where you care about odd numbers. So you should start with one outer “if else” testing for odd or even. No need to repeatedly test for even. Commented Jan 9, 2023 at 8:42
  • Every time you've written else { if you could just write else if, with one less } at the end. You also don't need the very last else. Commented Jan 9, 2023 at 8:47

2 Answers 2

1

I think an optimize version could be this :

if (N % 2 == 1 || (N >= 6 && N <= 20)) {
  System.out.println("Weird");
}
else {
  System.out.println("Not Weird");
}

If N is odd or N in range of 6 to 20 it's weird.

Else N is either even or not in the range so it's not weird.

Sign up to request clarification or add additional context in comments.

Comments

1

You can reduce the complexity and improve the readability of your code by extracting your logic to method/class etc. Also, nested conditions are hard to read, you should avoid it.

Example:

public static void main(String[] args) {

    int n = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    scanner.close();

    String print = isGivenNumberWeird(n) ? "Weird" : "Not Weird"
    System.out.println(print);
}

private static boolean isGivenNumberWeird(int n) {
    boolean isOdd = n % 2 == 1;
    if (isOdd) {
        return true;
    }
    if (n >= 2 && n <=5) {
        return false;
    }
    if (n >= 6 && n <=20) {
        return true;
    }
    if (n > 20) {
        return false;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.