简体   繁体   中英

Conditional Statement on Java7

Hello i just started a coding exercise on hackerrank and i am having a little challenge using scanner class with the skip function. here is what i have tried. Objective In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video!

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 20, print Not Weird
  • Complete the stub code provided in your editor to print whether or not n is weird.

Input Format

A single line containing a positive integer,n.

Constraints

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

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();

        if (N % 2 == 0 && N >= 2 && N <= 5 && N > 20) {
            System.out.println("not weird");

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

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


        scanner.close();
    }
}

Please what am i doing wrong.

You're mixing up && and || , so your first if will never run as mentioned in the comments.

So it looks like the only "Not Weird" print out is 2 , 4 and even numbers > 20 .

So use this to your advantage to just check for the "Weird" outputs, otherwise print "Not Weird" .

if (n % 2 == 0) {
    if ((n >= 2 && n <= 5) || (n > 20)) {
        return "Not Weird";
    }
}
return "Weird";

Online Demo

Having said this, I'm not sure what you want with Scanner::skip

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

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

This is how I simplified it.

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

See code below<\/i>

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

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

} else if (N % 2 == 0 && N >= 6 && N <= 20) {
    System.out.println("Weird");
} 
else 
{
    System.out.println("Weird");
}
["
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])?");
     
        if (n%2==0 && n>2 && n<5 || n>20) {
            System.out.println("Not Weird");
        } else if( n>6 || n<20) {
            System.out.println("Weird");
        } else {
            System.out.println("Weird");
        }
    
        scanner.close();
    }
    
}
["
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])?");

    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 {
            System.out.println("Weird");
             }        


    scanner.close();
}
}
["
package hudai;
import java.util.Scanner;
public class Hudai {   
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 == 1){
       System.out.println("Weird"); 
    } 
    if(N%2==0 && 2<N && N<5){
       System.out.println("Not Weird");
    }
    if(N%2==0 && 6<N && N<=20){
       System.out.println("Weird"); 
    }
    if(N%2==0 && 20<N && N<=100){
       System.out.println("Not Weird"); 
    }
}
   if(N % 2 == 0){
        if((N >=2 && N <= 5) || (N > 20)){
            System.out.println("Not Weird");
        }
        else if(N >= 6 && N <= 20){
            System.out.println("Weird");
        }   
    }
    else{
        System.out.println("Weird");
    }
["
import java.util.Scanner;

public class Main {


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

    public static void main(String[] args) {
        int n = scanner.nextInt();
        if (n % 2 == 1) {
            System.out.println("Weird");
        }
        if (n % 2 == 0 && 2 < n && n < 5) {
            System.out.println("Not Weird");
        }
        if (n % 2 == 0 && 6 < n && n <= 20) {
            System.out.println("Weird");
        }
        if (n % 2 == 0 && 20 < n && n <= 100) {
            System.out.println("Not Weird");

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

        }
    }
}
["
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();
      
        if (N % 2 == 0 && (N >= 2 && N <= 5) )
        {
            System.out.println("Not Weird");
        } 
        else if(N % 2==0 && N > 20)
        {
          System.out.println("Not Weird");  
        }
       else if ((N % 2 == 0) && (N >= 6 && N <= 20)) {
            System.out.println("Weird");
        } 
        else
        {
            System.out.println("Weird");
        }
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        scanner.close();
    }
}
["
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();
       
         if (N % 2 == 0 && N >= 2 && N <= 5 ) {
            System.out.println("Not Weird");

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

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

        scanner.close();
    }
}

Using ternary operation,

String w="Weird";
String nw= "Not Weird";
String s= (N%2!=0)?w:(N%2==0 && (N>=2 && N<=5))?nw:(N%2==0 && (N>=6 && N<=20))?w:nw;
if(n % 2 == 0){     
  if (n >= 2 && n <= 5 || n > 20) {
     System.out.println("Not Weird");
  }
  if (n >= 6 && n <= 20) {
     System.out.println("Weird");
  }   
} else if(n % 2 != 0){
     System.out.println("Weird");
 }
["
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])?");

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

   }
    else {
        System.out.println("Weird");
    }




    scanner.close();
}

Java 8 functional interface applied on Integer.

int n = 24;   
Function<Integer, String> f = x -> x % 2 == 1 || (x >= 6 && x <= 20) ? "Weird" : "Not Weird"; 
System.out.println(f.apply(n));
["

int N = scanner.nextInt();<\/i>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM