簡體   English   中英

如何修復 Java 中的錯誤源文件錯誤?

[英]how to fix a bad source file error in Java?

這些是我的代碼:

public class Solution {

    public boolean isBalanced(String s) {
      Stack<Character> stack = new Stack<Character>();
        
        char c;
        for(int i = 0; i<s.length(); i++) {
            c = s.charAt(i);
            if(c == '[' || c == '(') {
                stack.push(c);
            }
            
            else if(c == ']' || c == ')'){
                if(stack.isEmpty()) {
                    return false;
                }
                
                else if(c == ']' && stack.peek() == '[') {
                    stack.pop();}
                else if(c == ')' && stack.peek() == '(') {
                    stack.pop();
                }
                else {
                    return false;
                }
            if (stack.isEmpty()){
                return true;
            }   
            }
            
            }
        
        return false;
  }

    
    public static void main(String[] args) {

        
        Solution solution = new Solution();
        System.out.println(solution.isBalanced("()")); // should be true
        System.out.println(solution.isBalanced("(")); // should be false
        System.out.println(solution.isBalanced("(])")); // should be false

    }

}

我收到了這個錯誤:

Solution.java:4: error: duplicate class: a22629.Solution
public class Solution {
^

MyTests.java:6: error: cannot access Solution
Solution solution;
^

bad source file: ./Solution.java
file does not contain class Solution
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors
Not compiled

這是我得到的完整錯誤,我不知道為什么。

當我在終端中編譯它時它可以工作,但是當我想將它提交到我的 class 門戶時,它不起作用並給我這個錯誤。

請分享更多代碼或添加您成功編譯和調用的命令行數據。

可能的原因

  1. 您擁有 class 的default訪問權限 scope。 確保您的 class 是public的,以便 package 之外的其他人可以訪問。 (盡管在本地您仍然可以獨立編譯和運行 class,但 package 之外的其他類無法訪問您的類)
  2. 檢查測試預期的 package 結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM