2015-04-01 40 views
1

其實我是編程新手,但我試過這個簡單的數學遊戲。在這個程序中,我使s1,s2,s3,s4 & s5動態生成..但只有s1,s2正在改變,s3,s4,s5不會改變,如果方法再次調用...請幫助我。我有5個字符串有些正在動態變化,但有些不能

public class MainActivity extends Activity implements OnClickListener { 
Button b1, b2, b3; 
ImageView i, i2, i3; 
TextView t1, t2, t3; 
String s1, s2, s3, s4, s5; 

int m, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, n[]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b1 = (Button) findViewById(R.id.button1); 
    b2 = (Button) findViewById(R.id.button2); 
    b3 = (Button) findViewById(R.id.button3); 

    i = (ImageView) findViewById(R.id.imageView1); 
    t1 = (TextView) findViewById(R.id.textView1); 
    t2 = (TextView) findViewById(R.id.textView2); 
    t3 = (TextView) findViewById(R.id.textView3); 
    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    imp(); 

} 

public void imp() { 
    Random r1 = new Random(); 
    r2 = r1.nextInt(15) + 1; 
    r3 = r1.nextInt(15) + 1; 
    r4 = r2 + r3; 
    r5 = r2 - r3; 
    r6 = r2 * r3; 

    n = new int[] { r4, r5, r6 }; 

    while (r9 == r11 || r9 == r13 || r11 == r13) { 
     r8 = r1.nextInt(3) + 0; 
     r9 = n[r8]; 
     r10 = r1.nextInt(3) + 0; 
     r11 = n[r10]; 
     r12 = r1.nextInt(3) + 0; 
     r13 = n[r12]; 
    } 
    s1 = Integer.toString(r2); 
    s2 = Integer.toString(r3); 
    s3 = Integer.toString(r9); 
    s4 = Integer.toString(r11); 
    s5 = Integer.toString(r13); 
    t1.setText(s1); 
    t2.setText(s2); 
    b1.setText(s3); 
    b2.setText(s4); 
    b3.setText(s5); 

    int[] images = { R.drawable.plus, R.drawable.minus, R.drawable.multiply }; 

    Random random = new Random(); 
    m = images[random.nextInt(images.length)]; 
    i.setImageResource(m); 
} 

public void click(int k, Button b) { 
    if (m == R.drawable.plus && k == r4 || m == R.drawable.minus && k == r5 
      || m == R.drawable.multiply && k == r6) { 

     imp(); 

    } else { 
     t3.setText("FINISH"); 
     t3.setVisibility(View.VISIBLE); 
     b.setPressed(true); 
     b1.setClickable(false); 
     b2.setClickable(false); 
     b3.setClickable(false); 

    } 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button1: 
     click(r9, b1); 

     break; 
    case R.id.button2: 
     click(r11, b2); 
     break; 
    case R.id.button3: 
     click(r13, b3); 
     break; 
    } 
    } 
} 

和佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_horizontal|center_vertical" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="46dp" 
    android:layout_weight="0.97" 
    android:editable="false" 
    android:text="Large Text" 
    android:gravity="center" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<ImageView 
    android:layout_weight="1" 
    android:id="@+id/imageView1" 
    android:layout_width="67dp" 
    android:layout_height="29dp" 
    android:src="@drawable/plus" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="1.00" 
    android:text="Large Text" 
    android:gravity="center" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

<LinearLayout 
    android:paddingTop="50dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
<Button 
     android:layout_weight="1" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 
<Button 
    android:layout_weight="1" 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

<Button 
    android:layout_weight="1" 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

</LinearLayout> 

<TextView 
android:id="@+id/textView3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingTop="50dp" 
android:text="Large Text" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:textSize="50dp" 
android:visibility="invisible"/> 

</LinearLayout> 
+1

r9,r11,r13是否在變化?你會在這裏學習調試技巧。 – 323go 2015-04-01 19:05:54

回答

1

看看你的while循環中,通過這種方法運行第二次,病情不可能,因爲從上一次的結果通過被記住是真實的。我會建議使用局部變量而不是字段來解答這些問題。您可以通過將r9,r11和r13中的每一個設置爲0來繼續使用字段;然而,他們沒有充足的理由成爲田地。您只需要這些變量,直到您設置按鈕文本。現在,對於局部變量,你需要將它們初始化爲0,它們沒有像字段那樣的默認值。

如果您不介意我也提供一些不請自來的建議,我建議您使用稍微更具描述性的變量名稱。例如:

while (result1 == result2 || result1 == result3 || result2 == result3) { 
    randomIndex1 = random.nextInt(3) + 0; 
    result1 = resultArray[randomIndex1]; 
    randomIndex2 = random.nextInt(3) + 0; 
    result2 = resultArray[randomIndex2]; 
    randomIndex3 = random.nextInt(3) + 0; 
    result3 = resultArray[randomIndex3]; 
} 

這會導致代碼更容易理解其他人。 (並且爲了將來的自己!)

相關問題