2014-06-13 8 views
-7

이 코드가 나에게이 문제를주는 이유를 알고 싶습니다.이 프로젝트가 이전 양식의 동일한 프로젝트에서 이미 작동했지만 여기서는 작동하지 않습니다. 나 다른 방법을 사용하여 jCheckBox 값을 계산하기위한 어떤 방법이 있는지 그 밖의 오류가없는 경우?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){           
if (jCheckBox1.isSelected()== true) 
jCheckBox1.equals(56); 
if (jCheckBox2.isSelected()== true) 
jCheckBox2.equals(50); 
if (jCheckBox3.isSelected()== true) 
jCheckBox3.equals(56); 
if (jCheckBox4.isSelected()== true) 
jCheckBox4.equals(56); 
if (jCheckBox5.isSelected()== true) 
jCheckBox5.equals(56); 
if (jCheckBox6.isSelected()== true) 
jCheckBox6.equals(56); 
new Form6().setVisible(true); 

else 
if (jCheckBox1.isSelected()== false) 
jCheckBox1.equals(0); 
if (jCheckBox2.isSelected()== false) 
jCheckBox2.equals(0); 
if (jCheckBox3.isSelected()== false) 
jCheckBox3.equals(0); 
if (jCheckBox4.isSelected()== false) 
jCheckBox4.equals(0); 
if (jCheckBox5.isSelected()== false) 
jCheckBox5.equals(0); 
if (jCheckBox6.isSelected()== false) 
jCheckBox6.equals(0); 

JOptionPane.showMessageDialog(this, "Please Choose An Option and Try Again"); 

그리고

는 내가 배울 매우 열망입니다. 내 교수는 그가 자바 넷빈과 물건에 대해 거의 모든 것을 알고 있다고 말하지만 그는 지금까지 도움이별로 없습니다.

+3

초보자 모두에 괄호를 사용해야하는 이유는 자신의 경우 문 (나는 공간을 절약하기 위해 인라인 그것을 할 것을 선호합니다). 'if (jCheckBox6.isSelected() == false) {jCheckBox6.equals (0); }' 기본값으로 두지 말고 고급 조작해야합니다. – Gavin42

+1

@ Gavin42 : 초보자 만? –

+4

개인적으로, 나는 그 (것)들을 시간의 99.9 % 이용한다. 나는 블록이 시작하고 끝나는 곳을 시각적으로 확인하는 것을 선호한다. – Gavin42

답변

1

가의가 제대로 분리 들여 쓰기를 사용하여 코드의 조건부 구조를 좀 더 자세히 살펴 보자 무슨 일이 벌어지고 있는지 분명히 알 수 있습니다. 그와 지금

private void main(void){ 
    //Indentation shows how program blocks are related to one another 
    if (this.condition(parameters) == true) 
     // Indentation here shows the following statement is clearly associated with the above condition. 
     this.DoSomething(parameters); 

, 마음의 당신의 코드를 살펴 보자 :

여기
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){           

    if (jCheckBox1.isSelected()== true) 
     jCheckBox1.equals(56); 

    if (jCheckBox2.isSelected()== true) 
     jCheckBox2.equals(50); 

    if (jCheckBox3.isSelected()== true) 
     jCheckBox3.equals(56); 

    if (jCheckBox4.isSelected()== true) 
     jCheckBox4.equals(56); 

    if (jCheckBox5.isSelected()== true) 
     jCheckBox5.equals(56); 

    if (jCheckBox6.isSelected()== true) 
     jCheckBox6.equals(56); 

    new Form6().setVisible(true); // This statement will always run because it's not associated with a condition. 

    else // Oops! Here is an else that is not associated with an if statement! This probably doesn't compile 
     if (jCheckBox1.isSelected()== false) // This if is conditional on the else above. 
      jCheckBox1.equals(0); 

    if (jCheckBox2.isSelected()== false) 
     jCheckBox2.equals(0); 

    if (jCheckBox3.isSelected()== false) 
     jCheckBox3.equals(0); 

    if (jCheckBox4.isSelected()== false) 
     jCheckBox4.equals(0); 

    if (jCheckBox5.isSelected()== false) 
     jCheckBox5.equals(0); 

    if (jCheckBox6.isSelected()== false) 
     jCheckBox6.equals(0); 

    JOptionPane.showMessageDialog(this, "Please Choose An Option and Try Again"); 
} // I assume you mean to close the method 

내가 볼 것입니다 -이 코드는 조건 코드를 연결 {} 블록을 사용하지 않습니다. 괜찮 으면 좋겠지 만 {} 블록을 사용하지 않으면 if 문 다음에 매우 다음 행만 실행됩니다.

예 :

if (someCondition) 
    this.runSingleLine(); 

if (someCondition) 
    this.runSingleLine(); 
else 
    this.runSomeOtherSingleLine(); 


if (someCondition) 
{ 
    this.doSomething(); 
    this.doSomethingElse(); 
    ... 
    this.finishProcedure(); 
} 
10

사용 curly brackets (braces)에 들여 쓰기가에게 사소한 실수의 종류를 피할 수 있습니다. "이 때문에 무의미에 둘 다 - 위에서 구문 오류 설명하지만

코드

if (jCheckBox6.isSelected()== true) 
jCheckBox6.equals(56); 
new Form6().setVisible(true); 

else 
if (jCheckBox1.isSelected()== false) 
jCheckBox1.equals(0); 

, 얻어진 프로그램이 여전히 무의미한 것이다

if (jCheckBox6.isSelected()== true) { 
    jCheckBox6.equals(56); 
} 
new Form6().setVisible(true); // <-- WHAT??? 

else if (jCheckBox1.isSelected()== false) { // <-- WHERE IS MY IF? 
    jCheckBox1.equals(0); 
} 

동등 다른 "페어링 및 모든 chekbox.equals(string) 문은 아무 것도 (또는 의미하는) 의미가 없으므로 그러나 문제 설명이없는 추측은 신중하지 않은 것처럼 보입니다.

+2

+1 프로그램 혼동 해설. – christopher

0

이전 프로젝트에서 이것이 어떻게 작동했는지 나는 알 수 없습니다.

else 
      if (jCheckBox1.isSelected()== false) 

그리고이 점은 무엇인가 :

이 다른 문

자체 (NO 해당 if 문)에 의해인가? 당신은 참조로이 새로운 객체를 할당되지 않은 : 나는에 공백의 비트를 추가 할

:

new Form6().setVisible(true);