2014-12-01 3 views
-4

내가 부울 값을 전달하려고하는 것처럼 컴파일 할 수 없다. 첫 번째는자바 패싱 부울 값

public class Date { 
    public int m; 
    public int d; 
    public int y; 
    boolean isLeapYear; 

    public String monthIs(){ 
     return month; 
     m = Integer.parseInt(month); 
    } 

    public String dayIs(){ 
     return day; 
     d = Integer.parseInt(day); 
    } 

    public Date(String year){ 
     y = Integer.parseInt(year); 
     // Is y Divisible by 4 
     if (y % 4 == 0){ 

      // Is y Divisible by 4 but not 100 
      if (y % 100 != 0) 
       isLeapYear = true; 

      // Is y Divisible by 4 and 100 and 400 
      else if (y % 400 == 0) 
       isLeapYear = true; 

      // It's Divisible by 4 and 100 but not 400 
      else 
       isLeapYear = false; 
     } 
     // It's not divisible by 4 
     else 
     { 
      isLeapYear = false; 
      public boolean getisLeapYear() 
      { 
       return isLeapYear; 
      } 
     } 
    } 
} 

DateJDialog 클래스 나에게 어떤 이해가되지 않는이 오류가있다 : 당신은 당신의 코드에서 몇 가지 오류가

import javax.swing.JOptionPane; 
/** This program runs the Date class to determine if 
* the date entered falls within a leap year. 
*/ 
public class DateJDialog 
{ 
    public static void main(String[] args) 
    { 
     String month; 
     String day; 
     String year; 
     boolean isitLeapYear; 
     Date date; 
     //Get Input 
     JOptionPane.showMessageDialog(null, "This program determines if the date 
              entered falls within a leap year."); 
     month = JOptionPane.showInputDialog("What month?"); 
     day = JOptionPane.showInputDialog("What day?"); 
     year = JOptionPane.showInputDialog("What year?"); 

     //Create Date object 
     date = new Date(year); 

     if (date.getisLeapYear()==true); 
     if (isLeapYear = true) 
      JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year 
      + " does fall within a leap year."); 
     else 
      JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year 
      + " does not fall within a leap year."); 
     System.exit(0); 
    } 
} 
+9

1 단계 : 사람이 읽을 수 있도록 코드를 포맷하십시오. 2 단계 : 오류 메시지를 확인하십시오. 오류가 발생하면 포기하지 마십시오. * 문제를 해결하기 위해 * 오류를 읽으십시오. – David

+2

다른 메소드의 중간에 메소드 ('getIsLeapYear')를 정의 할 수 없습니다. – ajb

+0

isLeapYear == true 대신'isLeapYear == true'를 할 필요가 있습니다. 또한 항상 오류를 게시하십시오. 화면에 표시되는 내용을 추측하지 마십시오. – nos

답변

1

을, 나는 가리 키도록 최선을 다하겠습니다 그들 밖으로.

첫째,이 if-statement 있습니다

if (date.getisLeapYear()==true); 

if-statement 중괄호 중괄호 열기와 닫기로 이루어진 몸을 가지고 있어야합니다 (I이 컴파일 것이라고 생각하지만, 어떤 용도로 사용하지 않음). 대신이이 boolean 게터을 것으로 보인다 때문에

if (date.getisLeapYear()==true) { 
    //do something 
} 

, 조건 확인을 단축 할 수 있습니다

if (date.getisLeapYear()) {//this checks for a "true" value, !date.getisLeapYear() checks for false 
    //do something 
} 

isLeapYear 사실 것에 대한 귀하의 검사가 잘못이며, 다시하지 않을 때는 길이에서 입력 필요한.

if (isLeapYear = true) 

은 다음과 같아야합니다

if (isLeapYear)//again checking if true. !isLeapYear would be checking for false. 

귀하의 생성자는 이상하고 완전히 잘못 내장되어 있습니다. 먼저 if-else 문을 사용하는 경우 은 중괄호를 사용해야합니다.

다음은 유효하지만 나쁜 관행으로 간주 :

if (condition) 
    //do soemthing 

다음은 괄호가 필요합니다

if (condition) { 

} else if (another condition) { 

} (...) 

마지막으로, 생성자 내부에 게터를 선언하고 있습니다. 메서드는 클래스 범위에서만 만들 수 있습니다. 즉 메서드 내에서 메서드를 만들 수 없습니다. 이 문제를 해결하려면

public Date() { 
    (...) 
} 

public boolean getisLeapYear() { 
    return isLeapYear; 
} 

을 또한, 단지 팁, 당신은 하나 개의 선언에서 변수의 체인 여러 인스턴스 수

String month, day, year; 

I :

String month; 
String day; 
String year; 

는 같이 쓸 수있다 그것이 당신의 모든 오류를 다루는 지 전혀 모르겠지만 그것은 건강한 출발점입니다.