2 luke003 luke003 于 2016.01.14 22:32 提问

java小白求解,下面的代码是按照书上的例子抄下来的,不明白为什么报错。

代码如下:主要问题就是报错的地方:已经用注释吧报错贴上:还看不懂这个报错,求大神帮助:
package Calendar;

import java.util.Scanner;

public class Calendar {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("请输入您想查询的年份:");

    int year = in.nextInt();

    System.out.println("请输入您想查询的月份:");

    int month = in.nextInt();

    rili(year,month);

}

public static void rili(int year,int month){
    rilitou(year,month);
    riliti(year,month);
}

public static void rilitou(int year,int month){
    System.out.println("\t\t\t"+year+"年"+month+"月"+"\t\t\t");
    System.out.println("===================================================");
    System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日");
}

public static void riliti(int year,int month){
    int shouri=diyitian(year,month);
    int quyuetianshu=yuetianshu(year,month);
    int i=0;
    for(i=0;i<shouri;i++){
        System.out.println("      ");

    }
    for(i=1;i<=quyuetianshu;i++){
        System.out.printf("%4d",i);
    }
    if((i+shouri)%7==0){
        System.out.println();
    }
    System.out.println();

}
public static int diyitian(int year,int month){
    final int qishiri = 3;
    int zongtianshu = quzongtianshu(year,month);
    return (zongtianshu+qishiri)%7;
}
public static int quzongtianshu(int year,int month){
    int total= 0;
    for(int i=1800;i<year;i++){
        if(runnian(i)){
            total+=366;
        }else{
            total+=365;
        }
    }


    for(int i=1;i<month;i++){
        total = total+yuetianshu(year,i);

    }
    return total;
}
public static int yuetianshu(int year,int month){
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
        return 31;
    }
    if(month ==4||month==6||month==9||month==11){
        return 30;
    }
    if(month==2){
        return runnian(year)?29:28;
    }else{
    return 0;
    }
    public static boolean runnian(int year){

    /**
    *Multiple markers at this line
     Duplicate local variable year
     Syntax error on token ")", ; expected
     Syntax error on token "(", ; expected
     Illegal modifier for parameter runnian; only final is 
     permitted
        */

        return year%400==0||(year%4==0&&year%100!=0);



        /**Multiple markers at this line
           The operator % is undefined for the argument type(s) 
           boolean, int
           Cannot cast from int to boolean
           Type mismatch: cannot convert from boolean to int
        */
    }
}

}

3个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.01.14 22:56
已采纳

看着括号不匹配了吧

 if(month==2){
        return runnian(year)?29:28;
    }else{
    return 0;
    }  这里是不是少了},runnian这个方法在yuetianshu里面了。
    public static boolean runnian(int year){
caozhy
caozhy   Ds   Rxr 2016.01.14 22:42

boolean
->
Boolean

qq_19558705
qq_19558705   Ds   Rxr 2016.01.14 22:55

public static boolean runnian(int year) 前面差一个 “}”

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!