所以我正在尝试制作圆形计算器。一个人需要添加半径,直径,S或P,程序应该给出所有答案,但是当我添加一个东西时,程序会使用我添加到程序中的所有公式。我希望程序使用那些适合于那种情况的特定公式,例如,当我输入radius(r)时,程序将使用我为其添加的那些公式(//插入r时)而不是其他公式。 我希望你们明白我的意思,抱歉我的英语不好,希望有人可以提供帮助。
public void onButtonClick(View v) {
EditText a1 = (EditText) findViewById(R.id.TFnum1);
EditText a2 = (EditText) findViewById(R.id.TFnum2);
EditText a3 = (EditText) findViewById(R.id.TFnum6);
EditText a4 = (EditText) findViewById(R.id.TFnum7);
TextView tv = (TextView) findViewById(R.id.TFnum7); //P
TextView tv1 = (TextView) findViewById(R.id.TFnum6); //S
TextView tv2 = (TextView) findViewById(R.id.TFnum2); //d
TextView tv3 = (TextView) findViewById(R.id.TFnum1); //r
boolean flag = false;
double num1, num2, num6, num7, ans;
num1 = ParseDouble(a1.getText().toString());
num2 = ParseDouble(a2.getText().toString());
num6 = ParseDouble(a3.getText().toString());
num7 = ParseDouble(a4.getText().toString());
ans = 0;
//when inserting r
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = 2 * num1;
tv2.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = 3.14 * (num1 * num1);
tv1.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num1 * 3.14 * 2;
tv.setText(ans + "");
//when inserting d
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num2 / 2;
tv3.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num2 / 2) * (num2 / 2) * 3.14;
tv1.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num2 / 2) * 3.14 * 2;
tv.setText(ans + "");
//when inserting S
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = Math.sqrt(num6 / 3.14);
tv3.setText(ans + "");
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = Math.sqrt(num6 / 3.14) * 2;
tv2.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (Math.sqrt(num6 / 3.14)) * 2 * 3.14;
tv.setText(ans + "");
//when inserting P
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num7 / 6.28;
tv3.setText(ans + "");
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num7 / 6.28) * 2;
tv2.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num7 / 6.28) * (num7 /6.28) * 3.14;
tv1.setText(ans + "");
}
private double ParseDouble(String number) {
if (number!= null && number.length() > 0) {
try {
return Double.parseDouble(number);
} catch(Exception e) {
return -1;// Will return -1 in case of exception, you can change it with another value
}
}
return 1;
}
答案 0 :(得分:0)
问题是你拥有" onClick()"中的所有方法。功能。所以它贯穿每一个。
我建议你这样做。
您可以创建4个方法,分别对应每个参数(R,S,D,P)。然后在你的onClick()函数中,你将检查输入的位置,所以如果" R"输入后,您将调用相应的方法并发送相应的视图。
我希望下面的代码能更好地解释它:
全局声明以下内容(即在方法之外):
boolean flag = false;
TextView tv;
TextView tv1;
TextView tv2;
TextView tv3;
public void onButtonClick(View v) {
EditText a1 = (EditText) findViewById(R.id.TFnum1);
EditText a2 = (EditText) findViewById(R.id.TFnum2);
EditText a3 = (EditText) findViewById(R.id.TFnum6);
EditText a4 = (EditText) findViewById(R.id.TFnum6);
tv = (TextView) findViewById(R.id.TFnum7); //P
tv1 = (TextView) findViewById(R.id.TFnum6); //S
tv2 = (TextView) findViewById(R.id.TFnum2); //d
tv3 = (TextView) findViewById(R.id.TFnum1); //r
double num1, num2, num6, num7, ans;
num1 = ParseDouble(a1.getText().toString());
num2 = ParseDouble(a2.getText().toString());
num6 = ParseDouble(a3.getText().toString());
num7 = ParseDouble(a4.getText().toString());
ans = 0;
//this is where you will check whether values were entered
//and also depending on which EditText is accepting the R value
//for argument sake
if(!a1.equals("")){
onInsertR(v, num2, num1, ans);
}
if(!a2.equals ("")){onInsertR(v, num2,num1,ans);}
if(!a3.equals ("")){onInsertR(v, num2,num1,ans);}
if(!a4.equals ("")){onInsertR(v, num2,num1,ans);}
}
public void onInsertR(View v, double num2, double num1, double ans){
//when inserting r
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = 2 * num1;
tv2.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = 3.14 * (num1 * num1);
tv1.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num1 * 3.14 * 2;
tv.setText(ans + "");
}
public void onInsertD(View v, double num2,double ans){
//when inserting d
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num2 / 2;
tv3.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num2 / 2) * (num2 / 2) * 3.14;
tv1.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num2 / 2) * 3.14 * 2;
tv.setText(ans + "");
}
public void onInsertS(View v, double num2, double num6,double ans){
//when inserting S
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = Math.sqrt(num6 / 3.14);
tv3.setText(ans + "");
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = Math.sqrt(num6 / 3.14) * 2;
tv2.setText(ans + "");
//P
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (Math.sqrt(num6 / 3.14)) * 2 * 3.14;
tv.setText(ans + "");
}
public void onInsertP(View v, double num2, double num7,double ans){
//when inserting P
//r
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = num7 / 6.28;
tv3.setText(ans + "");
//d
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num7 / 6.28) * 2;
tv2.setText(ans + "");
//S
if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
else ans = (num7 / 6.28) * (num7 /6.28) * 3.14;
tv1.setText(ans + "");
}
private double ParseDouble(String number) {
if (number!= null && number.length() > 0) {
try {
return Double.parseDouble(number);
} catch(Exception e) {
return -1;// Will return -1 in case of exception, you can change it with another value
}
}
return 1;
}
希望这可以帮助你。
让我知道它是怎么回事:))
一切顺利!