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

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

java compile time error while solving array problem

问题

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

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. int tc = Integer.parseInt(br.readLine().trim());
  7. while (tc-- > 0) {
  8. String[] inputLine;
  9. int n = Integer.parseInt(br.readLine().trim());
  10. int[] arr = new int[n];
  11. inputLine = br.readLine().trim().split(" ");
  12. for (int i = 0; i < n; i++) {
  13. arr[i] = Integer.parseInt(inputLine[i]);
  14. }
  15. int ans = new Solution().print2largest(arr, n);
  16. System.out.println(ans);
  17. }
  18. }
  19. }
  20. // } Driver Code Ends
  21. //User function Template for Java
  22. class Solution {
  23. int print2largest(int arr[], int n) {
  24. int large = getLargest(arr, n);
  25. int res = -1;
  26. for (int i = 0; i < n; i++) {
  27. if (arr[i] != arr[large]) {
  28. if (res == -1) {
  29. res = arr[i];
  30. } else if (arr[i] > arr[res]) {
  31. res = arr[i];
  32. }
  33. }
  34. }
  35. return res;
  36. }
  37. public static int getLargest(int arr[], int n) {
  38. int l = 0;
  39. for (int i = 0; i < n; i++) {
  40. if (arr[i] > arr[l]) {
  41. l = i;
  42. }
  43. }
  44. return l;
  45. }
  46. }

I got the following errors

  1. prog.java:53: error: class, interface, or enum expected
  2. public static int getLargest(int arr[], int n) {
  3. ^
  4. prog.java:55: error: class, interface, or enum expected
  5. for(int i=0; i<n; i++){
  6. ^
  7. prog.java:55: error: class, interface, or enum expected
  8. for(int i=0; i<n; i++){
  9. ^
  10. prog.java:55: error: class, interface, or enum expected
  11. for(int i=0; i<n; i++){
  12. ^
  13. prog.java:58: error: class, interface, or enum expected
  14. }
  15. ^

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

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. int tc = Integer.parseInt(br.readLine().trim());
  7. while (tc-- &gt; 0) {
  8. String[] inputLine;
  9. int n = Integer.parseInt(br.readLine().trim());
  10. int[] arr = new int[n];
  11. inputLine = br.readLine().trim().split(&quot; &quot;);
  12. for (int i = 0; i &lt; n; i++) {
  13. arr[i] = Integer.parseInt(inputLine[i]);
  14. }
  15. int ans = new Solution().print2largest(arr, n);
  16. System.out.println(ans);
  17. }
  18. }
  19. }
  20. // } Driver Code Ends
  21. //User function Template for Java
  22. class Solution {
  23. int print2largest(int arr[], int n) {
  24. int large = getLargest(arr,n);
  25. int res = -1;
  26. for(int i=0;i&lt;n;i++){
  27. if(arr[i]!= arr[large]){
  28. if(res == -1){
  29. res = arr[i];
  30. }
  31. else if(arr[i]&gt;arr[res]){
  32. res = arr[i];
  33. }
  34. }
  35. }
  36. return res;
  37. }
  38. }
  39. public static int getLargest(int arr[], int n) {
  40. int l = 0;
  41. for(int i=0; i&lt;n; i++){
  42. if(arr[i]&gt;arr[l]){
  43. l = i;
  44. }
  45. }
  46. return l;
  47. }

I got the following errors

  1. prog.java:53: error: class, interface, or enum expected
  2. public static int getLargest(int arr[], int n) {
  3. ^
  4. prog.java:55: error: class, interface, or enum expected
  5. for(int i=0; i&lt;n; i++){
  6. ^
  7. prog.java:55: error: class, interface, or enum expected
  8. for(int i=0; i&lt;n; i++){
  9. ^
  10. prog.java:55: error: class, interface, or enum expected
  11. for(int i=0; i&lt;n; i++){
  12. ^
  13. prog.java:58: error: class, interface, or enum expected
  14. }
  15. ^

`

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类中,它将能够编译。

  1. public static int getLargest(int arr[], int n) {
  2. int l = 0;
  3. for(int i = 0; i < n; i++){
  4. if(arr[i] > arr[l]){
  5. l = i;
  6. }
  7. }
  8. return l;
  9. }

你的代码还有另一个错误:

  1. if(arr[i] != arr[large]){
  2. if(res == -1){
  3. res = arr[i]; // 你正在保存元素而不是索引
  4. }
  5. else if(arr[i] > arr[res]){ // 但你正在与索引比较,可能会导致数组越界异常。应该像这样 arr[i] > res
  6. res = arr[i]; // 你正在保存元素而不是索引
  7. }
  8. }
英文:

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

  1. public static int getLargest(int arr[], int n) {
  2. int l = 0;
  3. for(int i=0; i&lt;n; i++){
  4. if(arr[i]&gt;arr[l]){
  5. l = i;
  6. }
  7. }
  8. return l;
  9. }

There is another mistake with your code:

  1. if(arr[i]!= arr[large]){
  2. if(res == -1){
  3. res = arr[i]; // you are saving element not index
  4. }
  5. 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
  6. res = arr[i]; // you are saving element not index
  7. }
  8. }

答案2

得分: 1

方法必须位于类结构内。

声明类(Java™教程 > 学习Java语言 > 类和对象)

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. int tc = Integer.parseInt(br.readLine().trim());
  7. while (tc-- > 0) {
  8. String[] inputLine;
  9. int n = Integer.parseInt(br.readLine().trim());
  10. int[] arr = new int[n];
  11. inputLine = br.readLine().trim().split(" ");
  12. for (int i = 0; i < n; i++) {
  13. arr[i] = Integer.parseInt(inputLine[i]);
  14. }
  15. int ans = new Solution().print2largest(arr, n);
  16. System.out.println(ans);
  17. }
  18. }
  19. }
  20. // } Driver Code Ends
  21. //User function Template for Java
  22. class Solution {
  23. int print2largest(int arr[], int n) {
  24. int large = getLargest(arr, n);
  25. int res = -1;
  26. for (int i = 0; i < n; i++) {
  27. if (arr[i] != arr[large]) {
  28. if (res == -1) {
  29. res = arr[i];
  30. } else if (arr[i] > arr[res]) {
  31. res = arr[i];
  32. }
  33. }
  34. }
  35. return res;
  36. }
  37. public static int getLargest(int arr[], int n) {
  38. int l = 0;
  39. for (int i = 0; i < n; i++) {
  40. if (arr[i] > arr[l]) {
  41. l = i;
  42. }
  43. }
  44. return l;
  45. }
  46. }
英文:

Methods must be within a class structure.

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

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. int tc = Integer.parseInt(br.readLine().trim());
  7. while (tc-- &gt; 0) {
  8. String[] inputLine;
  9. int n = Integer.parseInt(br.readLine().trim());
  10. int[] arr = new int[n];
  11. inputLine = br.readLine().trim().split(&quot; &quot;);
  12. for (int i = 0; i &lt; n; i++) {
  13. arr[i] = Integer.parseInt(inputLine[i]);
  14. }
  15. int ans = new Solution().print2largest(arr, n);
  16. System.out.println(ans);
  17. }
  18. }
  19. }
  20. // } Driver Code Ends
  21. //User function Template for Java
  22. class Solution {
  23. int print2largest(int arr[], int n) {
  24. int large = getLargest(arr,n);
  25. int res = -1;
  26. for(int i=0;i&lt;n;i++){
  27. if(arr[i]!= arr[large]){
  28. if(res == -1){
  29. res = arr[i];
  30. }
  31. else if(arr[i]&gt;arr[res]){
  32. res = arr[i];
  33. }
  34. }
  35. }
  36. return res;
  37. }
  38. public static int getLargest(int arr[], int n) {
  39. int l = 0;
  40. for(int i=0; i&lt;n; i++){
  41. if(arr[i]&gt;arr[l]){
  42. l = i;
  43. }
  44. }
  45. return l;
  46. }
  47. }

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:

确定