بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
چرا متد فاکتوریل در بخش سوم، از جای خاصی باید صدا زده شود؟
با عرض سلام و خسته نباشید خدمت استاد عزیزم.
من نتوانستم علت ایراد خط 33 رو بفهمم در حالیکه همون مطلب در خط 113 آمده ولی این بار نتیجه فاکتوریل از درون event مربوط به btncompute صدا زده شده است و بدون مشکل اجرا میشود . دلیل اینکه از خارج از event یعنی از همون بالا نمی توان نتیجه فاکتوریل را صدا زد چیست ؟ ممنون
package noor.mahdi.apps;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class UiActivity extends Activity {
public int counter1;
public int counter2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtcaption = (TextView) findViewById(R.id.txtcaption);
final Button btnchangevisibility = (Button) findViewById(R.id.btnchangevisibility);
final Button btnchangevalue = (Button) findViewById(R.id.btnchangevalue);
final Button btncompute = (Button) findViewById(R.id.btncompute);
final EditText edtinput = (EditText) findViewById(R.id.edtinput);
//double Factorialresult = factorial(doubleuserinput);
//Log.i("log", "Factorialresult is :" + Factorialresult);
//edtinput.setText("some value");
for (int i = 0; i < 3; i++) {
Toast.makeText(UiActivity.this, "my message" + i, Toast.LENGTH_SHORT).show();
txtcaption.setText("i is:" + i);
}
txtcaption.setTextColor(Color.parseColor("#00ff00"));
txtcaption.setBackgroundColor(Color.parseColor("#7f7f7f"));
txtcaption.setTextSize(40);
txtcaption.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtcaption.setBackgroundColor(Color.parseColor("#0000ff"));
Toast.makeText(UiActivity.this, "تکست کلیک شد", Toast.LENGTH_SHORT).show();
Log.i("log", " تکست کلیک شد در لاگ ثبت شد ");
txtcaption.setText("Counter is:" + counter1);
counter1++;
Log.i("log", " Counter is : " + counter1);
}
});
btnchangevisibility.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
btnchangevisibility.setBackgroundColor(Color.parseColor("#ffff00"));
Toast.makeText(UiActivity.this, "دکمه کلیک شد", Toast.LENGTH_SHORT).show();
Log.i("log", " دکمه کلیک شد در لاگ ثبت شد ");
btnchangevisibility.setText("Counter is:" + counter2);
counter2++;
Log.i("log", " Counter is : " + counter2);
if (txtcaption.getVisibility() == View.INVISIBLE) {
txtcaption.setVisibility(View.VISIBLE);
} else {
txtcaption.setVisibility(View.INVISIBLE);
}
}
});
btnchangevalue.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
txtcaption.setBackgroundColor(Color.parseColor("#0000ff"));
Toast.makeText(UiActivity.this, "تکست کلیک شد", Toast.LENGTH_SHORT).show();
Log.i("log", " تکست کلیک شد در لاگ ثبت شد ");
//txtcaption.setText("Counter is:" + counter1);
String value = edtinput.getText().toString();
txtcaption.setText("your text:" + value + "" + counter1);
counter1++;
Log.i("log", " Counter is : " + counter1);
}
});
btncompute.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String userinput = edtinput.getText().toString();
double doubleuserinput = Double.parseDouble(userinput);
double Factorialresult = factorial(doubleuserinput);
txtcaption.setText("Factorial Of" + " " + doubleuserinput + " " + "is:" + Factorialresult);
Log.i("log", "Factorialresult Of" + " " + doubleuserinput + " " + "is:" + " " + Factorialresult);
}
});
}
public double factorial(double doubleuserinput) {
double result = 1;
for (double i = doubleuserinput; i > 1; i--) {
result = result * i;
}
return result;
}
}
برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال
uncocoder
12 سال پیش
0
0
در قسمت اول، شما بدون اینکه منتظر ورودی کاربر باشید، فاکتوریل را می خواهید حساب کنید، چون کد به محض اجرای نرم افزار، اجرا می شود، اما داخل EditText در آن لحظه چیزی نیست که بشود با آن محاسبه انجام داد.
در قسمت دوم، محاسبه پس از فشردن کلیدی انجام می شود که در این شرایط ممکن است محتوای EditText توسط کاربر تغییر کرده باشد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .