Eclipse裡的JAVA程式碼不知錯誤哪裡

以下是程式碼

package workshop.bmi;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

private EditText textheight;

private EditText textweight;

private TextView comment;

private Button btnok;

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

getViews();

setListeners();

}

private void setListeners() {

// TODO Auto-generated method stub

btnok.setOnClickListener(calcBMI);

}

private void getViews() {

// TODO Auto-generated method stub

textheight = (EditText)findViewById(R.id.editHeight);

textweight = (EditText)findViewById(R.id.editWeight);

comment = (TextView)findViewById(R.id.textResult);

btnok = (Button)findViewById(R.id.btnDo);

}

private OnClickListener calcBMI = new OnClickListener(){

@Override

public void onClick(View arg0) {

double stdweight, height, weight, BMI;

DecimalFormat nf = new DecimalFormat("0.0");

height = Double.parseDouble(textheight.getText().toString());

weight = Double.parseDouble(textweight.getText().toString());

BMI = weight * 100 * 100 / (height * height);

RadioButton r = null;

r = (RadioButton)findViewById(R.id.radioMan);

if (r.isChecked()==true)

stdweight = (height-80)* 0.7;

else

stdweight = (height-70)* 0.6;

//Give health comment

comment.setText("你的BMI 值是:" + nf.format(BMI)+ "\n" + "理想體重值為:"+ nf.format(stdweight));

}

}

Update:

public class MainActivity extends Activity {

private EditText textheight;

private EditText textweight;

private TextView comment;

private Button btnok;

} ←這個括號出現了"Syntax error on token "}", delete this token"的錯誤,但看不懂

Update 2:

程式碼最後的括號出現了"Syntax error, insert "}" to complete ClassBody"的錯誤,但也看不懂

Update 3:

希望有高人指點,讓正確的程式碼完成

Update 4:

把上面那個括號拿掉,留下面那個再試一次看看?

拿掉之後就只剩下最後一個括號出現了"Syntax error, insert "}" to complete ClassBody"的錯誤

2 Answers

Rating
  • Lv 4
    8 years ago
    Favorite Answer

    您好:

    您上面貼出來的程式碼.有2個寫法上的錯誤.因此無法通過編譯.

    錯誤1..

    public class MainActivity extends Activity {

    private EditText textheight;

    private EditText textweight;

    private TextView comment;

    private Button btnok;

    }

    MainActivity 他既是class .就不應該 把它當成 main ..於下方加入了

    大括符 .因此請把大括符} 拿到本 class 的結尾處..即最尾巴.

    錯誤2..

    private OnClickListener calcBMI = new OnClickListener(){

    他是一個物件...因此在結尾時請別忘了加上( 分號 ; )

    也就是說 請刪掉 紅色記號的大括符.

    並於最尾端

    comment.setText("你的BMI 值是:" + nf.format(BMI)+ "\n" + "理想體重值為:"+ nf.format(stdweight));

    }

    } ;

    }

    加入分號及class 的結尾大括符.

    Source(s): 經驗
  • 8 years ago

    把上面那個括號拿掉,留下面那個再試一次看看?

    2012-06-16 22:27:30 補充:

    那你最後再補一個 } 試試

    2012-06-17 06:40:08 補充:

    public class MainActivity extends Activity { <<這是主要的類別吧

    ...

    } <<但應該這裡還沒結束,所以去掉這個右括號

    @Override

    ...

    //下面這裡應該是原兇,寫法忘記叫什麼了.還有要確定O這個的方法是不是都有漏掉

    private OnClickListener calcBMI = new OnClickListener(){

    ...

    } << 少了個 ; 號

    <<最後再補上 } 號

    猜的囧

Still have questions? Get your answers by asking now.