为什么不能编译?

时间:2015-01-17 01:36:15

标签: java

//我不明白错误信息,我觉得有些不对劲 //这是一个使用格里高利历图书馆的程序

import java.util.Scanner; 
import java.io.*;         
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Locale;
import static java.lang.System.*;

public class jcal {
  static final GregorianCalendar CHANGE_DATE
           = new GregorianCalendar (1,GregorianCalendar.JANUARY, 1);
 public static void main(String[]args) {
 GregorianCalendar c = new GregorianCalendar();
 c.setGregorianChange (CHANGE_DATE.getTime());

 int cmonth;
 int cyear;
 if (args.length > 0) {
    cmonth = Integer.parseInt (args[0]) -1;
    cyear = Integer.parseInt (args[1]);
 }else{
    //use the current month
    cmonth = c.get (GregorianCalendar.MONTH);
    cyear = c.get (GregorianCalendar.YEAR);
 }
 c.set (cyear, cmonth, 1);

 //while loop for printing out the month of january
 while (cmonth == c.get (GregorianCalendar.MONTH)) {
     int cday = c.get (GregorianCalendar.DAY_OF_MONTH); 
     int wkday = c.get (GregorianCalendar.DAY_OF_WEEK);
     out.printf ("%04d/%02d/%02d is weekday %d. %n",
                 cyear, cmonth + 1, cday, wkday);
     c.add (GregorianCalendar.DAY_OF_MONTH, 1);
     }

    }

  }

}

^这些括号的正确数量是多少?还有一个,少一个? 什么是解析?

1 个答案:

答案 0 :(得分:2)

您的错误是您在末尾有一个额外的右括号(})。

对于任何想知道的人,是的,我是在自己的电脑上编写的,因为我对生活没有任何好处。

计算机科学应用程序:我们可以在没有编译器的情况下检查这个。这是典型的堆栈ADT问题。请你的教授向你解释一下:

import java.util.*;

class aclass
{
    public static void main (String[] args)
    {
        String code = "import java.util.Scanner; import java.io.*; import java.util.GregorianCalendar; import java.util.Calendar; import java.util.Locale; import static java.lang.System.*; public class jcal { static final GregorianCalendar CHANGE_DATE = new GregorianCalendar (1,GregorianCalendar.JANUARY, 1); public static void main(String[]args) { GregorianCalendar c = new GregorianCalendar(); c.setGregorianChange (CHANGE_DATE.getTime()); int cmonth; int cyear; if (args.length > 0) { cmonth = Integer.parseInt (args[0]) -1; cyear = Integer.parseInt (args[1]); }else{ //use the current month cmonth = c.get (GregorianCalendar.MONTH); cyear = c.get (GregorianCalendar.YEAR); } c.set (cyear, cmonth, 1); //while loop for printing out the month of january while (cmonth == c.get (GregorianCalendar.MONTH)) { int cday = c.get (GregorianCalendar.DAY_OF_MONTH); int wkday = c.get (GregorianCalendar.DAY_OF_WEEK); out.printf (\"%04d/%02d/%02d is weekday %d. %n\", cyear, cmonth + 1, cday, wkday); c.add (GregorianCalendar.DAY_OF_MONTH, 1); } } } }";

        System.out.println("Length of your code: " + code.length());
        Stack stack = new Stack();
        char character;
        for (int i = 0; i < code.length(); i++)
        {
            character = code.charAt(i);
            if (character == '{')
            {
                stack.push(1);
            } else if (character == '}') {
                if (stack.isEmpty()) { System.out.println("You have an extra } at position " + i); break; } //oh no
                stack.pop();
            }
        }
        if (!stack.isEmpty()) { System.out.println("You're missing " + stack.size() + " }s"); }
    }
}

Running example。这是输出:

  

代码长度:1019

     

你在1018号位置有一个额外的}

很明显,最后一个字符是额外的}