Java编译时错误,而解决数组问题[关闭]

w6lpcovy  于 2023-06-20  发布在  Java
关注(0)|答案(2)|浏览(89)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
昨天关门了。
Improve this question
我是Java新手,不明白我在这里做错了什么。

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tc = Integer.parseInt(br.readLine().trim());
        while (tc-- > 0) {
            String[] inputLine;
            int n = Integer.parseInt(br.readLine().trim());
            int[] arr = new int[n];
            inputLine = br.readLine().trim().split(" ");
            for (int i = 0; i < n; i++) {
                arr[i] = Integer.parseInt(inputLine[i]);
            }

            int ans = new Solution().print2largest(arr, n);
            System.out.println(ans);
        }
    }
}

// } Driver Code Ends

//User function Template for Java

class Solution {
    int print2largest(int arr[], int n) {
        int large = getLargest(arr,n);
        int res = -1;
        for(int i=0;i<n;i++){
            if(arr[i]!= arr[large]){
                if(res == -1){
                    res = arr[i];
                }
                else if(arr[i]>arr[res]){
                    res = arr[i];
                }
            }
        }
        return res;
    }
}

public static int getLargest(int arr[], int n) {
    int l = 0;
    for(int i=0; i<n; i++){
        if(arr[i]>arr[l]){
            l = i;
        }
    }
    return l;
}

我得到以下错误

prog.java:53: error: class, interface, or enum expected
public static int getLargest(int arr[], int n) {
              ^
prog.java:55: error: class, interface, or enum expected
    for(int i=0; i<n; i++){
    ^
prog.java:55: error: class, interface, or enum expected
    for(int i=0; i<n; i++){
                 ^
prog.java:55: error: class, interface, or enum expected
    for(int i=0; i<n; i++){
                      ^
prog.java:58: error: class, interface, or enum expected
        }
        ^


这是我的输入--> 6 12 35 1 10 34 1
我期望给定数组的结果是--> 34

ix0qys7i

ix0qys7i1#

此方法不在类中。在Java中,方法应该是类的一部分。把它放在Solution类中,它会编译。

public static int getLargest(int arr[], int n) {
int l = 0;
  for(int i=0; i<n; i++){
    if(arr[i]>arr[l]){
        l = i;
    }
  }
 return l;
}

你的代码还有一个错误:

if(arr[i]!= arr[large]){
            if(res == -1){
                res = arr[i]; // you are saving element not index
            }
            else if(arr[i]>arr[res]){ // but you are comparing with index, it can give arrayOutOfBound exception. It should be like this arr[i]>res
                res = arr[i]; // you are saving element not index
            }
        }
xqnpmsa8

xqnpmsa82#

方法必须在类结构中。

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int tc = Integer.parseInt(br.readLine().trim());
        while (tc-- > 0) {
            String[] inputLine;
            int n = Integer.parseInt(br.readLine().trim());
            int[] arr = new int[n];
            inputLine = br.readLine().trim().split(" ");
            for (int i = 0; i < n; i++) {
                arr[i] = Integer.parseInt(inputLine[i]);
            }

            int ans = new Solution().print2largest(arr, n);
            System.out.println(ans);
        }
    }
}

// } Driver Code Ends

//User function Template for Java

class Solution {
    int print2largest(int arr[], int n) {
        int large = getLargest(arr,n);
        int res = -1;
        for(int i=0;i<n;i++){
            if(arr[i]!= arr[large]){
                if(res == -1){
                    res = arr[i];
                }
                else if(arr[i]>arr[res]){
                    res = arr[i];
                }
            }
        }
        return res;
    }

    public static int getLargest(int arr[], int n) {
        int l = 0;
        for(int i=0; i<n; i++){
            if(arr[i]>arr[l]){
                l = i;
            }
        }
        return l;
    }
}

相关问题