【问题标题】:Error <identifier> expected for System.out.println(); ? How can I fix this?System.out.println() 应出现错误 <标识符>; ?我怎样才能解决这个问题?
【发布时间】:2022-06-19 15:41:18
【问题描述】:

为什么我会收到一个错误提示 >error: identifier expected System.out.println(); 。我需要在数组中的最后一个元素之后换行,这就是我添加 println 的原因。我该如何解决这个问题?

     import java.util.Scanner;
public class LabProgram {
    public static void main(String args[]) {
    int n, left,right;
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        int a[] = new int[n];
        for(int i = 0; i < n; i++)
            a[i] = s.nextInt();
        left=s.nextInt();
        right=s.nextInt();
        for(int i = 0; i < n; i++)
            //check if the current element is within the range
            if(a[i]>=left && a[i]<=right)
                System.out.print(a[i] + ",");
    }
    System.out.println();
}

【问题讨论】:

  • 代码必须在方法中。

标签: java compiler-errors newline identifier


【解决方案1】:
SOP written outside the main method. It must be inside the main method. 

import java.util.Scanner;  
public class LabProgram {  
    public static void main(String args[]) {  
    int n, left,right;  
        Scanner s = new Scanner(System.in);  
        n = s.nextInt();  
        int a[] = new int[n];  
        for(int i = 0; i < n; i++)  
            a[i] = s.nextInt();  
        left=s.nextInt();  
        right=s.nextInt();  
        for(int i = 0; i < n; i++) { 
            //check if the current element is within the range  
            if(a[i]>=left && a[i]<=right)  
                System.out.print(a[i] + ",");  
    }  
    System.out.println();  
}  
} 

【讨论】:

    【解决方案2】:

    CandidateCode.java:16: 错误: ')' 预期 System.out.println(gifts.stream().limit(n).reduce(0L, Long()sum) ); ^ CandidateCode.java:16:错误:';'预期的 System.out.println(gifts.stream().limit(n).reduce(0L, Long()sum) ); ^ CandidateCode.java:16:错误:表达式的非法开始 System.out.println(gifts.stream().limit(n).reduce(0L, Long()sum) ); ^

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多