Results 1 to 3 of 3
- 02-07-2017, 05:36 AM #1
Senior Member
- Join Date
- Dec 2016
- Posts
- 103
- Rep Power
- 0
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
- 02-07-2017, 10:47 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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
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 **
- 02-07-2017, 03:54 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Task Complete But Saying This Is Wrong
And somewhere you need to do a lower bounds check of 2.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Run Task and Update Progress while Waiting for task.get();
By cjburkey01 in forum JavaFXReplies: 2Last Post: 05-20-2016, 04:53 PM -
Complete Noob
By Phans in forum New To JavaReplies: 10Last Post: 01-31-2014, 08:56 PM -
Complete Beginner
By rjreynolds in forum New To JavaReplies: 4Last Post: 01-12-2012, 04:33 AM -
Jar file not complete
By desoky725 in forum NetBeansReplies: 6Last Post: 09-23-2011, 08:33 AM -
Download the Complete API?
By JDCAce in forum NetBeansReplies: 2Last Post: 10-02-2008, 02:02 AM
Bookmarks