簡體   English   中英

我的程序符合要求,但不打印任何內容

[英]My program complies but doesn't print anything

我寫這個程序來檢查,如果{ [ ( " /*與平衡} ] ) " */我們需要忽略的東西在注釋塊。 當我在終端中運行代碼時,我遇到一個問題,什么也沒打印出來。 我知道錯誤在哪里,但我不知道為什么會出錯。 在嘗試編寫忽略注釋的代碼后,我開始遇到這個奇怪的問題。 我們需要寫出自己的名為MyStack.java的堆棧類,但是它非常簡單,因此在此不做介紹。

我認為問題發生在這里:

int start = str.indexOf("/*");
int end = str.indexOf("*/");
if(start != -1){
  str = str.substring(0,start) + str.substring(end+2);

這是整個代碼:我希望沒有任何邏輯上的謬誤

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class SymbolBalance {

    public static void main(String[] args){
        if(args.length>0){
            try{
                Scanner input = new Scanner(new File (args[0]));
                MyStack<Character> ms = new MyStack<>();
                String str;
                char ch;
                boolean quoteStart = true;
                loop: while(input.hasNext()){
                    str = input.next();
                    int start = str.indexOf("/*");
                    int end = str.indexOf("/*");
                    if(start != -1){
                      str = str.substring(0,start) + str.substring(end+2);
                        for(int i=0;i<str.length();i++){
                        ch = str.charAt(i);      
                            if(ch == '{'||ch =='(' || ch=='[')     
                                {
                               ms.push(ch);
                                }
                            else if((ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*')){
                                ms.push(str.charAt(i+1));
                            }

                            else if (ch==')'){
                                if(ms.isEmpty()||ms.pop()!= '('){
                                   System.out.println(") is mismatched");
                                   break loop;
                                }
                            }
                           else if(ch == ']'){
                                if(ms.isEmpty() || ms.pop() != '['){
                                    System.out.println("] is mismatched");
                                    break loop;
                                }      
                           }
                            else if(ch == '}'){
                                if(ms.isEmpty() || ms.pop() != '{'){
                                    System.out.println("} is mismatched");
                                    break loop;
                                }
                            }
                            else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
                                if(ms.isEmpty() || ms.pop() != '*'){
                                    System.out.println("*/ is mismatched");
                                    break loop;
                                }   
                            }
                           else if(ch == '"'){
                                if(quoteStart == true) {
                                    ms.push(ch);
                                    quoteStart = false;
                               }
                                else {
                                    if(ms.isEmpty() || ms.pop() != '"'){
                                        System.out.println(" \" is mismatched");  
                                        break loop;
                                    }
                                    quoteStart = true;
                                }
                            }  
                       }
                    }
                }
                input.close();
            }
            catch(FileNotFoundException e){
                   System.out.println("Cannot find file");
            }

        }
        else{
               System.out.println("No command line argument");
        }
    }

}

在這里,我包括一個示例輸入文件:

/* this is to test whether the program ignores imbalances in the comment blocks */
public class Test3 {
    public static void main(String[] args) {
        boolean haveYouWatchedHamiltonYet = true;
        int theSchuylerSisters = 3; 
        int alexanderhamilton = 1;
        int aaronburr = 1;

        boolean amIintheroom = theRoomWhereItHappens();


        /* this is a commented block. We're testing if your code 
        can deal with unbalanced things in the comments: /*
        that was the test for here */
    }

    /*just a general comment */ 
    /* this one has some  errors /* { [  { [ } ] */   


    public boolean theRoomWhereItHappens() {
        boolean intheRoomWhereItHappens = false;
        boolean isHappyAboutThis = false;
        return intheRoomWhereItHappens && isHappyAboutThis;

    }
}

您在注釋的開頭和結尾使用相同的字符:

    int start = str.indexOf("/*");
    int end = str.indexOf("/*");

結束注釋字符串應為:

    int end = str.indexOf("*/");

暫無
暫無

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

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