简体   繁体   English

Java7 上的条件语句

[英]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.你好,我刚刚开始在 hackerrank 上进行编码练习,我在使用扫描仪 class 和跳过 function 时遇到了一些挑战。这是我尝试过的。 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:给定一个 integer,执行以下条件操作:

  • If n is odd, print Weird如果 n 是奇数,打印 Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird如果 n 是偶数且在 2 到 5 的范围内,则打印 Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird如果 n 是偶数并且在 6 到 20 的范围内,则打印 Weird
  • If n is even and greater than 20, print Not Weird如果 n 是偶数且大于 20,则打印 Not Weird
  • Complete the stub code provided in your editor to print whether or not n is weird.完成编辑器中提供的存根代码以打印 n 是否奇怪。

Input Format输入格式

A single line containing a positive integer,n.包含正数 integer,n 的单行。

Constraints约束条件

Output Format Output 格式

Print Weird if the number is weird;如果数字很奇怪,则打印 Weird; otherwise, print Not Weird.否则,打印 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. ,所以你的第一个if永远不会像评论中提到的那样运行。

So it looks like the only "Not Weird" print out is 2 , 4 and even numbers > 20 .所以看起来唯一的"Not Weird"打印出来的是24甚至数字> 20

So use this to your advantage to just check for the "Weird" outputs, otherwise print "Not Weird" .因此,您可以利用它来检查"Weird"输出,否则打印"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话虽如此,我不确定你想要什么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>请参阅下面的代码<\/b><\/p>

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.应用于 Integer 的 Java 8 功能接口。

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> int N = 扫描仪.nextInt();<\/b> scanner.skip("(\\r\\n|[\\n\\r\
\
\…])?");<\/i> scanner.skip("(\\r\\n|[\\n\\r\
\
\…])?");<\/b><\/p>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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