輸入三個不重複的數字 , 按下"送出猜測數字"的按鈕 , 即可在下方使用Toast來顯示猜測狀況 , 以下是程式碼。
package COM.TQC.GDD02; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class GDD02 extends Activity { /** Called when the activity is first created. */ Button submit; TextView respond; EditText input; int[] num = new int[3]; int count , a , b; String inputString , randomString , wrongString = "輸入錯誤! 請輸入三個不重複的數字"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); submit = (Button) this.findViewById(R.id.button1); respond = (TextView) this.findViewById(R.id.respond) ; input = (EditText) this.findViewById(R.id.input); do { randomString = String.valueOf((int)(Math.random()*1000)); }while(!checkNumber(randomString)); submit.setOnClickListener(BOnClickListener); Log.d("Test", "randomString:"+randomString); } public Button.OnClickListener BOnClickListener = new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub inputString = input.getText().toString(); if(inputString.length()==3 && checkNumber(inputString)) { if(matchNumber()) showToast("INPUT的數字:"+inputString+" 恭喜你!答對囉~"); else { showToast("INPUT的數字:"+inputString+"="+a+"A"+b+"B"); a=0; b=0; } } else { showToast(wrongString); input.setText(""); } } }; public boolean checkNumber(String number) { boolean pass = true; for(int i=0;i<2;i++) { for(int j=i+1;j<3;j++) { if(number.charAt(i)==number.charAt(j)) { pass = false; } } } return pass; } public boolean matchNumber() { // TODO Auto-generated method stub boolean pass = true; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(inputString.charAt(i)==randomString.charAt(j)) { if(i==j) a++; else b++; } } } if(a!=3) pass = false; return pass; } public void showToast(String Text) { Toast t = Toast.makeText(GDD02.this, "", Toast.LENGTH_LONG); t.setText(Text); t.show(); } }
很簡單的Layout
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....
請問如果我是要結果不是用toast 而是要用 text view顯示 那要怎麼改呢?? 麻煩你了
回覆刪除