gpt4 book ai didi

java - HackerRank 代码中应有

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:59 26 4
gpt4 key购买 nike

我一直在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com