我一直在 hackerRank 上尝试此代码,但收到以下错误。
Solution.java:36: error: <identifier> expected
System.out.print(ans);
^
Solution.java:36: error: <identifier> expected
System.out.print(ans);
^
2 errors
代码:
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 scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
String ans="";
// if 'n' is NOT evenly divisible by 2 (i.e.: n is odd)
if(n%2==1){
ans = "Weird";
}
else if (n%2==0)//even no
{
if(n>2 && n<=5)
ans="Not Wierd";
}
if(n>6 && n<20)
{
ans="Wierd";
}
else
{
ans="Not Wierd";
}
}
System.out.print(ans);
}
我的错误是什么?
最佳答案
将 System.out.print(ans);
向上移动一行,使其位于 }
之前:
System.out.print(ans);
}
就目前情况而言,您的 System.out
代码位于 main()
之外。
关于java - HackerRank 代码中应有 <identifier>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362085/