Results 1 to 3 of 3
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Task Complete But Saying This Is Wrong

    Task
    Given an integer, , 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 , print Not Weird


    Java Code:
     import java.io.*;
        import java.util.*;
        import java.text.*;
        import java.math.*;
        import java.util.regex.*;
    
        public class Solution {
    
            public static void main(String[] args) {
    
                Scanner sc=new Scanner(System.in);
                int n=sc.nextInt();            
                String ans="";
                if(n%2==1){
                  ans = "Weird";
                }
                
                else if(n%2==0||n<=5){
                
                   ans ="Not Weird";
                }
                    
                
                else if(n%2==0||n<=6&&n<=20){
                
                   ans ="Weird";
                }
                else if(n%2==0||n>20){
                
                   ans ="Not Weird";
                }
                System.out.println(ans);
                
            }
        }

    but its Task3 and Task7 said this is wrong

    check this link please : https://www.hackerrank.com/challenges/java-if-else

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Task Complete But Saying This Is Wrong

    First off, get rid of all the checks for even.
    The first part of the 'if' has already determined that the number is odd, so all the else clauses have to be checking an even number.

    I'm assuming n has to be greater than 0? Otherwise what happens if n is less than 1?

    Anyway, have another look at this check:
    Java Code:
    n <= 6 && n <= 20
    Are you sure this is saying what you think it's saying?
    Also, as a note, use some more whitespace...it's free and makes the code easier to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Task Complete But Saying This Is Wrong

    And somewhere you need to do a lower bounds check of 2.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2016, 04:53 PM
  2. Complete Noob
    By Phans in forum New To Java
    Replies: 10
    Last Post: 01-31-2014, 08:56 PM
  3. Complete Beginner
    By rjreynolds in forum New To Java
    Replies: 4
    Last Post: 01-12-2012, 04:33 AM
  4. Jar file not complete
    By desoky725 in forum NetBeans
    Replies: 6
    Last Post: 09-23-2011, 08:33 AM
  5. Download the Complete API?
    By JDCAce in forum NetBeans
    Replies: 2
    Last Post: 10-02-2008, 02:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •