我是android編程的新手。我正在製作一個簡單的乘法器的小測試應用程序。 我的應用程序有一個TextView和三個buttons.Namely 乘以2,乘以3和清除。 代碼工作正常而倍增,但我希望循環10後結束,並且buttons..I 10日點擊嘗試了各種方法,但似乎沒有一個下班後的TextView應顯示「停止」。我不確定「Do-while」是否是一個好主意。 這裏的java代碼:循環應該在第10次點擊後停止,但不是
public class MainActivity extends Activity {
TextView Display;
Button X2, X3, Clear;
int a = 1, result;
String Over = "Stopped";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// a = 1;
X2 = (Button) findViewById(R.id.bX2);
X3 = (Button) findViewById(R.id.bX3);
Clear = (Button) findViewById(R.id.bClear);
Display = (TextView) findViewById(R.id.tvDisplay);
X2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// a = 1;
// this loop should stop after the 10th click of the button
// and the text view should display "stopped"
do {
result = a * 2;
Display.setText("2 X " + a + " = " + result);
a++;
} while (a == 0);
}
});
X3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// a = 1;
// this loop should stop after the 10th click of the button
// and the text view should display "stopped"
do {
result = a * 3;
Display.setText("3 X " + a + " = " + result);
a++;
} while (a == 0);
}
});
Clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
result = 0;
a = 1;
Display.setText("" + result);
}
});
}
}
和這裏的XML代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_vertical"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Result display"
android:textSize="25sp" />
<Button
android:layout_width="250dp"
android:layout_gravity="center"
android:id="@+id/bX2"
android:layout_height="wrap_content"
android:text="Multiply by 2" />
<Button
android:layout_width="250dp"
android:layout_gravity="center"
android:id="@+id/bX3"
android:layout_height="wrap_content"
android:text="Multiply by 3" />
<Button
android:layout_width="75dp"
android:layout_gravity="center"
android:id="@+id/bClear"
android:layout_height="wrap_content"
android:text="Clear" />
我不知道的發帖規則,因此,如果我原諒我我違反了任何規定。我真的被困在這裏,任何幫助都會是「很棒」。
謝謝。
但是你的'a'永遠不等於0.它有意義嗎? – nikis
循環的目的是什麼? – Triode
您的問題描述與您的代碼不符。如果你想在點擊上乘一次並且每次只做10次,那麼你不需要循環。你需要有一個實例計數器和增加/檢查計數器每次點擊等 – rnk