英文:
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-- > 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
答案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<n; i++){
if(arr[i]>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]>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
}
}
答案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.
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论