#Simple beginner problem help

45 messages · Page 1 of 1 (latest)

grizzled marsh
#

Stuck on this for a good half an hour at least, can't figure out what's wrong (on a website called Hackerrank)
My code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



   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();
       
       if(N % 2 == 0){
           System.out.println("Not Weird");}
           else if(N % 2 != 0){
               System.out.println("Weird");}
           else if(N % 2 == 0 && N >= 2 && N <= 5){
               System.out.println("Not Weird");}
           else if(N % 2 == 0 && N >= 6 && N <= 20 ){
               System.out.println("Weird");}
           else if(N % 2 == 0 && N > 20){
               System.out.println("Not Weird");}
           else if(N % 2 != 0){
               System.out.println("Weird");}
   }
}```
zealous quailBOT
#

This post has been reserved for your question.

Hey @grizzled marsh! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

agile orbit
#

So lets simplify the if statements first.

grizzled marsh
#

Alright

agile orbit
#
if (N % 2 != 0) {
  System.out.println("Weird");
}

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

So to simply, I've done the following.
The most broad case (If N is odd) is by itself.
The if/else chain is now only for Even Numbers.

grizzled marsh
#

Alright, makes sense so far

agile orbit
#

Since we already know N is even in the chain, we don't need to test it in every else if, since we've already been able to filter it out to that point.

grizzled marsh
#

I plugged your code into it

agile orbit
#

It will still fail

grizzled marsh
#

Yeah

agile orbit
#

I didn't fix your problem, I just made the code easier to read ;)

#

I will say all the conditions there are correct, but there is an issue with how the if/else chain is set up.

grizzled marsh
#

Yeah I can't really figure out the chain I wrote

#

Started learning java recently so I have a lot of trouble with the syntax and what not

agile orbit
#

It takes time. The main thing you need to remember in this case is that if/else is executed from Top to Bottom. So is there a reason for it to not get to the bottom of your chain?

grizzled marsh
agile orbit
#

Why would that stop it?

grizzled marsh
#

it just stops at the top

#

does it not?

#

if it's an even number it just stops there

agile orbit
#

And since we've already filtered the odd numbers out, all numbers will be even.

grizzled marsh
#

hold on Imma try to fix it

#

I did not fix it

agile orbit
#

Post the code

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

nbm

#

nvm

#

I'm blind

#

Okay I passed all the test cases, thanks!

zealous quailBOT
agile orbit
#

Also you probably don't need the .skip()

grizzled marsh
#

it was in there by default

agile orbit
#

Weird.

grizzled marsh
#

well, it was one of the recommended resources to learn java

#

or programming languages in general

#

hackerrank, that is

agile orbit
#

Whatever works for you. It gets easier, best of luck!

grizzled marsh
#

I'd like to try and avoid video tutorials since I always give up a few weeks in

agile orbit
#

Find a project you want to do (that is reasonable), and do it.

#

Helps you study the program with a target in mind, and a goal to end with to get the dopamine of finishing something.

grizzled marsh
#

Alright, thanks!