綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 比較數字大小

[複製連結] 查看: 2147|回覆: 5|好評: 0
跳轉到指定樓層
樓主
wind8894 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2012-2-26 21:42

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
本帖最後由 wind8894 於 2012-2-27 00:55 編輯

有三個EditText

使用者分別輸入數字

在Button事件下將最大的數字列出來

使用TextView顯示

問題:   1.能夠運行程式,但輸入完EditText的數字,按下button就會顯示錯誤,是不是輸入的數字沒寫入num1內呢?
  1. package white.shu.namespace;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.TextView;
  6. import android.widget.EditText;
  7. import android.view.View;

  8. import java.text.DecimalFormat;


  9. public class CompareActivity extends Activity
  10. {
  11.        
  12.         private EditText num1,num2,num3;
  13.         private TextView tv;
  14.         private Button button1;
  15.         protected TextView editText1;
  16.         private TextView editText2;
  17.         private TextView editText3;
  18.        
  19.        
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState)
  23.     {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);
  26.         
  27.         num1=(EditText)findViewById(R.id.editText1);
  28.         num2=(EditText)findViewById(R.id.editText2);
  29.         num3=(EditText)findViewById(R.id.editText3);
  30.             tv=(TextView)findViewById(R.id.textView1);
  31.             button1=(Button)findViewById(R.id.button1);
  32.            
  33.            
  34.            
  35.     }
  36.                     public void button_click(View view)
  37.                     {
  38.                     int max;
  39.            
  40.                     int num1 = Integer.parseInt(editText1.getText().toString());
  41.                     int num2=Integer.parseInt(editText2.getText().toString());
  42.                     int num3=Integer.parseInt(editText3.getText().toString());
  43.            
  44.    
  45.                     if(num1>num2&num1>num3)
  46.                     {
  47.                             max=num1;
  48.                     }                   
  49.                     else if (num2>num1&&num2>num3)
  50.                     {
  51.                             max=num2;
  52.                     }                   
  53.                     else
  54.                     {
  55.                             max=num3;
  56.                     }
  57.            
  58.            
  59.            
  60.                     DecimalFormat nf = new DecimalFormat("0.00");
  61.                    
  62.                     String fromfloat = String.valueOf(max);
  63.                     tv.setText("最大數為: "+nf.format(max));
  64.            
  65.             }
  66.    
  67. }
複製代碼
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
song6680 | 收聽TA | 只看該作者
發表於 2012-2-28 15:47
Button沒OnClickListener嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-2-28 19:01
不用OnClickListener可以把EditText的數字存入嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
song6680 | 收聽TA | 只看該作者
發表於 2012-2-28 19:06
button要OnClickListene觸發...點button才不會錯誤
而且他案button要做運算跟存入,所以一定要OnClickListener

評分

參與人數 1碎鑽 +1 幫助 +1 收起 理由
Lake + 1 + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-2-28 19:27
我弄好了 感謝喔 原來是宣告那邊沒宣告好
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
song6680 | 收聽TA | 只看該作者
發表於 2012-2-28 20:22
嗯嗯,其實你19~21的宣告可以不用

評分

參與人數 1碎鑽 +1 幫助 +1 收起 理由
Lake + 1 + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則