簡體   English   中英

為什么這個“其他”陳述不起作用?

[英]Why isn't this “else” statement working?

已編輯

這是整個程序。 我將char包裹在Character包裝器中以使用.equals()並解決了索引問題,並用文字替換了常量。 該程序可以編譯並正常運行,告訴您這些符號是否匹配,但是在存在不匹配的符號時仍會跳過該“ else”語句:

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


public class Exercise22_11 {

/**
 * 
 */
public static void main(String[] args) {

        /**for command line argument*/

        if(args.length == 0) {
            System.out.println("You didn't enter an argument dummy!");
            System.exit(0);
        }


        String fileString = "";

        /**Open the file and read it*/

        try{    
            File myOutFile = new File(args[0]);
            Scanner input = new Scanner(myOutFile);
            while (input.hasNext()){
        fileString += input.next();
            }

        }//try

        catch (FileNotFoundException e){
      System.out.println("Sorry that file is not found " + e);
        }//catch

        /**Build the list of characters from file*/

        char[] charArray = new char[fileString.length()];

        for (int i = 0; i < fileString.length(); i++) {
            charArray[i] = fileString.charAt(i);
        }//for building charArray

        /**Create a stack to manipulate grouping symbols*/

        Stack<Character> stack = new Stack<Character>();

        /**Pushes grouping symbols into stack and pops them when they correspond*/

        for (int i = 0; i < charArray.length; i++) {
            Character temp = new Character(charArray[i]);

            if (temp.equals('{') || temp.equals('(') || temp.equals('[')) {
                stack.push(temp);
            }   else if (temp.equals('}') || temp.equals(')') || temp.equals(']')) {
                if (temp.equals('}') && stack.peek().equals('{')){
                    stack.pop();
                }   else if (temp.equals(')') && stack.peek().equals('(')) {
                    stack.pop();
                }   else if (temp.equals(']') && stack.peek().equals('[')) {
                    stack.pop();
                }   else {
                    System.out.println("There is a mistake at index: " + i);
                    System.out.println("\nHere's the code (the error is in the middle):\n");
                    for(int j = i - 20; j <= i + 20; j++) {
                        System.out.print(charArray[j]);
                    }
                    System.out.println("\n");
                }
            }

        }//for

        /**Inform user of result*/

        if (stack.isEmpty()) {
            System.out.println("Congratulations!  Your grouping symbols are matched!");
        }
        else {
            System.out.println("I'm sorry.  Please fix your program.");
        }



}//main



}//class

發生錯誤時,將在運行時跳過最后的“ else”語句。

任務是使用列表或集合來編寫程序,以檢查程序中的分組符號是否重疊。 該文件應作為命令行參數輸入。

這是我問教授的內容(尚無答案):

  1. 為什么我不能創建一個掃描器來讀取.txt(或.java)文件中的每個字符? 我嘗試使用“”(無空格)分隔符來避免將Char []的RIDICULOUS字符串與stack()一起使用。

  2. friggin的“ else”語句是什么?

謝謝!

如果此打印語句用於調試,則不應將其放置在else 您希望將錯誤放入要修復(或彈出)的else之一中, 然后 在找到錯誤的位置進行打印。 擺脫其他聲明。 做了

}   else if (temp == SIX && stack.peek().equals(FIVE)) {
                    stack.pop();
                }   
    System.out.println("There is a mistake at index: " + charArray[i]); //it will work now

暫無
暫無

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

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