Java编译时出现解决数组问题的错误。

huangapple go评论66阅读模式
英文:

java compile time error while solving array problem

问题

I am new to Java can't understand what i did wrong here

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;
    }
}

I got the following errors

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
        }
        ^

This is my input -->
6
12 35 1 10 34 1

I am expecting this result for the given array -->
34

英文:

I am new to Java can't understand what i did wrong here

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-- &gt; 0) {
String[] inputLine;
int n = Integer.parseInt(br.readLine().trim());
int[] arr = new int[n];
inputLine = br.readLine().trim().split(&quot; &quot;);
for (int i = 0; i &lt; 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&lt;n;i++){
if(arr[i]!= arr[large]){
if(res == -1){
res = arr[i];
}
else if(arr[i]&gt;arr[res]){
res = arr[i];
}
}
}
return res;
}
}
public static int getLargest(int arr[], int n) {
int l = 0;
for(int i=0; i&lt;n; i++){
if(arr[i]&gt;arr[l]){
l = i;
}
}
return l;
}

I got the following errors

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&lt;n; i++){
^
prog.java:55: error: class, interface, or enum expected
for(int i=0; i&lt;n; i++){
^
prog.java:55: error: class, interface, or enum expected
for(int i=0; i&lt;n; i++){
^
prog.java:58: error: class, interface, or enum expected
}
^

`

This is my input -->
6
12 35 1 10 34 1

I am expecting this result for the given array -->
34

答案1

得分: 1

这个方法不属于任何类。
在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]; // 你正在保存元素而不是索引
    }
    else if(arr[i] > arr[res]){ // 但你正在与索引比较,可能会导致数组越界异常。应该像这样 arr[i] > res
        res = arr[i]; // 你正在保存元素而不是索引
    }
}
英文:

this method is out of classes.
In Java methods should be part of class.
Keep it in class Solution, It will compile.

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

There is another mistake with your code:

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

答案2

得分: 1

方法必须位于类结构内。

声明类(Java™教程 > 学习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;
    }
}
英文:

Methods must be within a class structure.

Declaring Classes (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Classes and Objects).

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-- &gt; 0) {
            String[] inputLine;
            int n = Integer.parseInt(br.readLine().trim());
            int[] arr = new int[n];
            inputLine = br.readLine().trim().split(&quot; &quot;);
            for (int i = 0; i &lt; 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&lt;n;i++){
            if(arr[i]!= arr[large]){
                if(res == -1){
                    res = arr[i];
                }
                else if(arr[i]&gt;arr[res]){
                    res = arr[i];
                }
            }
        }
        return res;
    }

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

huangapple
  • 本文由 发表于 2023年6月18日 20:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定