2014-02-24 74 views
1

我是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" /> 

我不知道的發帖規則,因此,如果我原諒我我違反了任何規定。我真的被困在這裏,任何幫助都會是「很棒」。

謝謝。

+1

但是你的'a'永遠不等於0.它有意義嗎? – nikis

+0

循環的目的是什麼? – Triode

+0

您的問題描述與您的代碼不符。如果你想在點擊上乘一次並且每次只做10次,那麼你不需要循環。你需要有一個實例計數器和增加/檢查計數器每次點擊等 – rnk

回答

1

你的循環執行的每次點擊

一個做而一直執行一次之前檢查條件的同時,也看不到相關的值的任何邏輯10

你可能想

if (a <= 10) { 
    //do some stuff 
} 

代替您do-while循環ŝ

+0

嗨尼克,感謝您的快速反應。但你建議我應該怎麼做?正如我曾經使用過的,也是一樣。 – Rahul

+0

用我貼的代碼替換你的循環 - 因爲你的'a'從1開始,循環只能執行一次。看來你*希望*價值只增加點擊,而不是自己,所以你不需要循環 –

+1

Voilla!它的工作..謝謝一噸。 :) – Rahul

0

你必須保持一個計數器和0開始初始化。

在您onClickListener增加一個該計數器變量值,並檢查它是否等於10.如果是這樣的話顯示停止。

private int counter; // Instance variable 

public void onClick(View v) { 
    counter++; 
    if(counter == 10) { 
     // display the stop on your textview here 
    } 
    ...do whatever you want here 
} 
0

按鈕的onClickListener執行每一個按鈕被點擊的時間。你需要在課堂上有一個變量作爲一個計數器來計算點擊次數。每次點擊該按鈕時,都會將此計數器加1。最後,當此計數器達到10時,您將設置TextView停止顯示。

而且,它可能是一個更好的選擇,使用「如果」條款,而不是做-一段時間。

+0

體面足夠的描述,但他們已經保持他們的變數在會員等級 –

-1
public class MainActivity extends Activity { 

TextView Display; 
Button X2, X3, Clear; 
int a = 1, result; 
String Over = "Stopped"; 
counter = 1; 

@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" 
      if(counter<10) 

       result = a * 2; 
       Display.setText("2 X " + a + " = " + result);     
       a++; 
       counter++     
      } 
      else{ 
        Display.setText("Stopped"); 
       } 
     } 
    }); 
+0

偉大的幫助脂肪酶..謝謝! – Rahul

+0

那麼爲什麼你給我一個負面的1?「? – murielK

+0

嘿,我可能已經按下了向下的箭頭而不是!!大聲笑..新的這裏。 – Rahul

相關問題