我收到Bracket错误,我只是想弄清楚。
按下按钮后代码应该有一个随机数,然后我应该有一个计数低于该数的计数器。我的目标最终是有一个胜利条件,检查数字是否相同,你赢了。但只需要帮助让theese托架正确对齐。 在Eclipse中为android做这个 任何帮助都会很棒谢谢!
package com.viralgamez.gastime;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class NewGame<Stopwatch> extends Activity {
private TextView displayRandInt;
private Button updateRandInt;
final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;
private static final Random rand = new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
/* Setup your Activity */
// Find the views (their id's should be specified in the XML layout file)
displayRandInt = (TextView) findViewById(R.id.displayRandInt);
updateRandInt = (Button) findViewById(R.id.updateRandInt);
// Give the Button an onClickListener
updateRandInt.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
int randInt = rand.nextInt(100)+1;
displayRandInt.setText(String.valueOf(randInt));
}
public void onClick2(View v) {
if(status == false)
{
btnStart.setText("Stop");
status = true;
new Thread(new Runnable()
{ run(){
for(int i=0; i < 500; i++)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
txtCounter.setText(String.valueOf(i));
}
});
Thread.sleep(1000);
}}
}).start();
}
else if (status == true)
{
btnStart.setText("Start");
status = false;
initCouner();
});
答案 0 :(得分:1)
我首先通过http://jsbeautifier.org/运行代码(对于JavaScript,但它缩进Java就好了)。您可以使用 Control + Shift + F 在Eclipse中执行此操作。如果每次添加新代码块时都这样做,那么代码将始终整洁。
从那里,很容易看出哪些括号匹配。首先,将run()
变为实际的方法签名非常重要。除此之外,您在else if
结束括号时遇到问题。
我已经从自动压缩器中音译了您的代码,并在底部添加了缺少的括号。我在一些你应该注意的地方评论过(方法签名和缺少括号,即)。
package com.viralgamez.gastime;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class NewGame<Stopwatch> extends Activity {
private TextView displayRandInt;
private Button updateRandInt;
final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;
private static final Random rand = new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
/* Setup your Activity */
// Find the views (their id's should be specified in the XML layout file)
displayRandInt = (TextView) findViewById(R.id.displayRandInt);
updateRandInt = (Button) findViewById(R.id.updateRandInt);
// Give the Button an onClickListener
updateRandInt.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
int randInt = rand.nextInt(100) + 1;
displayRandInt.setText(String.valueOf(randInt));
}
public void onClick2(View v) {
if (status == false) {
btnStart.setText("Stop");
status = true;
new Thread(new Runnable() {
// run() { // This needs to be changed to an actual method signature
public void run() {
for (int i = 0; i < 500; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
txtCounter.setText(String.valueOf(i));
}
});
Thread.sleep(1000);
}
}
}).start();
// } else if (status == true) { // Change this to just }else{
} else {
btnStart.setText("Start");
status = false;
initCouner();
// }); // This needs to be changed to the following:
}
}
});
}
}
答案 1 :(得分:0)
您似乎错过了else if
末尾的括号,以及onClick2
的另一个结束括号(Sam在我之前抓住了第二个)
答案 2 :(得分:0)
试试这个:
updateRandInt.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
}
public void onClick2(View v) {
if(status == false)
{
}
else if (status == true)
{
} // Not Here );
}
}); // Here
此后你错过了几个小括号。
答案 3 :(得分:0)
如前所述,您错过了右括号,而您的结束);
不合适,已在下面进行了更正。如果您正在使用eclipse,以便将来轻松解决此问题,只需在页面底部添加右括号,然后单击它们旁边的。 Eclipse会自动突出显示相应的打开和关闭括号。您可以这样做以正确对齐括号和括号,或者确定是否有太多括号或需要添加另一个括号。
//starting from the last else if statement
else if (status == true)
{
btnStart.setText("Start");
status = false;
initCouner();
}
}
});
}
}