这是我的Java代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private double counter;
private double sum = 0;
private String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handle click
TextView textGemiddelde = (TextView)
findViewById(R.id.Gemiddelde);
textGemiddelde.append(calculate());
}
});
}
public double sum() {
EditText EditText1 = (EditText) findViewById(R.id.EditText1);
double cijfer1 = Double.valueOf(EditText1.getText().toString());
EditText EditText2 = (EditText) findViewById(R.id.EditText2);
double cijfer2 = Double.valueOf(EditText2.getText().toString());
EditText EditText3 = (EditText) findViewById(R.id.EditText3);
double cijfer3 = Double.valueOf(EditText3.getText().toString());
EditText EditText4 = (EditText) findViewById(R.id.EditText4);
double cijfer4 = Double.valueOf(EditText4.getText().toString());
EditText EditText5 = (EditText) findViewById(R.id.EditText5);
double cijfer5 = Double.valueOf(EditText5.getText().toString());
EditText EditText6 = (EditText) findViewById(R.id.EditText6);
double cijfer6 = Double.valueOf(EditText6.getText().toString());
EditText EditText7 = (EditText) findViewById(R.id.EditText7);
double cijfer7 = Double.valueOf(EditText7.getText().toString());
EditText EditText8 = (EditText) findViewById(R.id.EditText8);
double cijfer8 = Double.valueOf(EditText8.getText().toString());
EditText EditText9 = (EditText) findViewById(R.id.EditText9);
double cijfer9 = Double.valueOf(EditText9.getText().toString());
EditText EditText10 = (EditText) findViewById(R.id.EditText10);
double cijfer10 = Double.valueOf(EditText10.getText().toString());
EditText EditText_1 = (EditText) findViewById(R.id.EditText_1);
double weging1 = Double.valueOf(EditText_1.getText().toString());
EditText EditText_2 = (EditText) findViewById(R.id.EditText_2);
double weging2 = Double.valueOf(EditText_2.getText().toString());
EditText EditText_3 = (EditText) findViewById(R.id.EditText_3);
double weging3 = Double.valueOf(EditText_3.getText().toString());
EditText EditText_4 = (EditText) findViewById(R.id.EditText_4);
double weging4 = Double.valueOf(EditText_4.getText().toString());
EditText EditText_5 = (EditText) findViewById(R.id.EditText_5);
double weging5 = Double.valueOf(EditText_5.getText().toString());
EditText EditText_6 = (EditText) findViewById(R.id.EditText_6);
double weging6 = Double.valueOf(EditText_6.getText().toString());
EditText EditText_7 = (EditText) findViewById(R.id.EditText_7);
double weging7 = Double.valueOf(EditText_7.getText().toString());
EditText EditText_8 = (EditText) findViewById(R.id.EditText_8);
double weging8 = Double.valueOf(EditText_8.getText().toString());
EditText EditText_9 = (EditText) findViewById(R.id.EditText_9);
double weging9 = Double.valueOf(EditText_9.getText().toString());
EditText EditText_10 = (EditText) findViewById(R.id.EditText_10);
double weging10 = Double.valueOf(EditText_10.getText().toString());
if(!TextUtils.isEmpty(EditText1.getText().toString()) &&
!TextUtils.isEmpty(EditText_1.getText().toString())) {
this.sum += cijfer1 * weging1;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText2.getText().toString()) &&
!TextUtils.isEmpty(EditText_2.getText().toString())) {
this.sum += cijfer2 * weging2;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText3.getText().toString()) &&
!TextUtils.isEmpty(EditText_3.getText().toString())) {
this.sum += cijfer3 * weging3;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText4.getText().toString()) &&
!TextUtils.isEmpty(EditText_4.getText().toString())) {
this.sum += cijfer4 * weging4;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText5.getText().toString()) &&
!TextUtils.isEmpty(EditText_5.getText().toString())) {
this.sum += cijfer5 * weging5;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText6.getText().toString()) &&
!TextUtils.isEmpty(EditText_6.getText().toString())) {
this.sum += cijfer6 * weging6;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText7.getText().toString()) &&
!TextUtils.isEmpty(EditText_7.getText().toString())) {
this.sum += cijfer7 * weging7;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText8.getText().toString()) &&
!TextUtils.isEmpty(EditText_8.getText().toString())) {
this.sum += cijfer8 * weging8;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText9.getText().toString()) &&
!TextUtils.isEmpty(EditText_9.getText().toString())) {
this.sum += cijfer9 * weging9;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText10.getText().toString()) && !TextUtils.isEmpty(EditText_10.getText().toString())) {
this.sum += cijfer10 * weging10;
this.counter++;
} else {
}
return sum;
}
public double average() {
return sum / counter;
}
public String calculate() {
value = String.valueOf(average());
return value;
}
}
这将是我的第一个应用程序,因此请输入乱码。它的基本作用如下:该应用程序有10个输入字段,您可以在其中输入数字,还有10个字段输入该数字的权重。如果单击底部按钮,它将计算输入数字+重量的平均值。当前,当我单击按钮时,以下内容将附加到“ textGemiddelde”(平均为荷兰语):NaN。如果有人能指出为什么我得到此代码,而不是给出的数字+权重的平均值,我将不胜感激。
答案 0 :(得分:0)
您不会调用<int-aws:request-handler-advice-chain>
方法,并且变量sum()
未初始化。
答案 1 :(得分:0)
发生NaN,因为您尝试计算0/0。此外,计数器未初始化。我已经改正了。告诉它是否有效。
public class MainActivity extends AppCompatActivity {
private double counter = 0;
private double sum = 0;
private String value;
TextView textGemiddelde;
EditText EditText1;
EditText EditText2;
EditText EditText3;
EditText EditText4;
EditText EditText5;
EditText EditText6;
EditText EditText7;
EditText EditText8;
EditText EditText9;
EditText EditText10;
EditText EditText_1;
EditText EditText_2;
EditText EditText_3;
EditText EditText_4;
EditText EditText_5;
EditText EditText_6;
EditText EditText_7;
EditText EditText_8;
EditText EditText_9;
EditText EditText_10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText1 = (EditText) findViewById(R.id.EditText1);
EditText2 = (EditText) findViewById(R.id.EditText2);
EditText3 = (EditText) findViewById(R.id.EditText3);
EditText4 = (EditText) findViewById(R.id.EditText4);
EditText5 = (EditText) findViewById(R.id.EditText5);
EditText6 = (EditText) findViewById(R.id.EditText6);
EditText7 = (EditText) findViewById(R.id.EditText7);
EditText8 = (EditText) findViewById(R.id.EditText8);
EditText9 = (EditText) findViewById(R.id.EditText9);
EditText10 = (EditText) findViewById(R.id.EditText10);
EditText_1 = (EditText) findViewById(R.id.EditText_1);
EditText_2 = (EditText) findViewById(R.id.EditText_2);
EditText_3 = (EditText) findViewById(R.id.EditText_3);
EditText_4 = (EditText) findViewById(R.id.EditText_4);
EditText_5 = (EditText) findViewById(R.id.EditText_5);
EditText_6 = (EditText) findViewById(R.id.EditText_6);
EditText_7 = (EditText) findViewById(R.id.EditText_7);
EditText_8 = (EditText) findViewById(R.id.EditText_8);
EditText_9 = (EditText) findViewById(R.id.EditText_9);
EditText_10 = (EditText) findViewById(R.id.EditText_10);
textGemiddelde = (TextView)
findViewById(R.id.Gemiddelde);
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handle click
sum = sum();
textGemiddelde.append(calculate());
}
});
}
public double sum() {
sum = 0;
counter = 0;
double cijfer1 = ParseDouble(EditText1.getText().toString());
double cijfer2 = ParseDouble(EditText2.getText().toString());
double cijfer3 = ParseDouble(EditText3.getText().toString());
double cijfer4 = ParseDouble(EditText4.getText().toString());
double cijfer5 = ParseDouble(EditText5.getText().toString());
double cijfer6 = ParseDouble(EditText6.getText().toString());
double cijfer7 = ParseDouble(EditText7.getText().toString());
double cijfer8 = ParseDouble(EditText8.getText().toString());
double cijfer9 = ParseDouble(EditText9.getText().toString());
double cijfer10 = ParseDouble(EditText10.getText().toString());
double weging1 = ParseDouble(EditText_1.getText().toString());
double weging2 = ParseDouble(EditText_2.getText().toString());
double weging3 = ParseDouble(EditText_3.getText().toString());
double weging4 = ParseDouble(EditText_4.getText().toString());
double weging5 = ParseDouble(EditText_5.getText().toString());
double weging6 = ParseDouble(EditText_6.getText().toString());
double weging7 = ParseDouble(EditText_7.getText().toString());
double weging8 = ParseDouble(EditText_8.getText().toString());
double weging9 = ParseDouble(EditText_9.getText().toString());
double weging10 = ParseDouble(EditText_10.getText().toString());
if(!TextUtils.isEmpty(EditText1.getText().toString()) &&
!TextUtils.isEmpty(EditText_1.getText().toString())) {
this.sum += cijfer1 * weging1;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText2.getText().toString()) &&
!TextUtils.isEmpty(EditText_2.getText().toString())) {
this.sum += cijfer2 * weging2;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText3.getText().toString()) &&
!TextUtils.isEmpty(EditText_3.getText().toString())) {
this.sum += cijfer3 * weging3;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText4.getText().toString()) &&
!TextUtils.isEmpty(EditText_4.getText().toString())) {
this.sum += cijfer4 * weging4;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText5.getText().toString()) &&
!TextUtils.isEmpty(EditText_5.getText().toString())) {
this.sum += cijfer5 * weging5;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText6.getText().toString()) &&
!TextUtils.isEmpty(EditText_6.getText().toString())) {
this.sum += cijfer6 * weging6;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText7.getText().toString()) &&
!TextUtils.isEmpty(EditText_7.getText().toString())) {
this.sum += cijfer7 * weging7;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText8.getText().toString()) &&
!TextUtils.isEmpty(EditText_8.getText().toString())) {
this.sum += cijfer8 * weging8;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText9.getText().toString()) &&
!TextUtils.isEmpty(EditText_9.getText().toString())) {
this.sum += cijfer9 * weging9;
this.counter++;
} else {
}
if(!TextUtils.isEmpty(EditText10.getText().toString()) && !TextUtils.isEmpty(EditText_10.getText().toString())) {
this.sum += cijfer10 * weging10;
this.counter++;
} else {
}
return sum;
}
public double average() {
if(couter == 0) return -1;
return sum / counter;
}
public String calculate() {
value = String.valueOf(average());
return value;
}
double ParseDouble(String strNumber) {
if (strNumber != null && strNumber.length() > 0) {
try {
return Double.parseDouble(strNumber);
} catch(Exception e) {
return -1; // or some value to mark this field is wrong. or make a function validates field first ...
}
}
else return 0;
}
}
答案 2 :(得分:0)
您需要调用sum()方法,以便sum和counter变量具有值。在当前代码中,它们为0,因为未调用sum()方法。因此,0/0返回NaN。 另外,每次单击按钮时,您都需要重置总和和计数器的值。否则,这些值将每次都加起来,因为它们是字段变量。 另外,您不必每次在sum()方法中都使用findViewById。 只需在onCreate中将其初始化一次即可,这将达到目的,因为此操作很繁琐。 要快速修复,请通过以下方式更改平均方法:
public double average() {
sum = 0;
counter = 0;
sum();
return sum / counter;
}
这将起作用。
但是,为了使代码更简洁明了,可以将类编写为:
class MainActivity extends AppCompatActivity {
private double counter;
private double sum = 0;
private String value;
private EditText mEditText1;
private EditText mEditText2;
private EditText mEditText3;
private EditText mEditText4;
private EditText mEditText5;
private EditText mEditText6;
private EditText mEditText7;
private EditText mEditText8;
private EditText mEditText9;
private EditText mEditText10;
private EditText mEditText_1;
private EditText mEditText_2;
private EditText mEditText_3;
private EditText mEditText_4;
private EditText mEditText_5;
private EditText mEditText_6;
private EditText mEditText_7;
private EditText mEditText_8;
private EditText mEditText_9;
private EditText mEditText_10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.Button);
mEditText1 = (EditText) findViewById(R.id.EditText1);
mEditText2 = (EditText) findViewById(R.id.EditText2);
mEditText3 = (EditText) findViewById(R.id.EditText3);
mEditText4 = (EditText) findViewById(R.id.EditText4);
mEditText5 = (EditText) findViewById(R.id.EditText5);
mEditText6 = (EditText) findViewById(R.id.EditText6);
mEditText7 = (EditText) findViewById(R.id.EditText7);
mEditText8 = (EditText) findViewById(R.id.EditText8);
mEditText9 = (EditText) findViewById(R.id.EditText9);
mEditText10 = (EditText) findViewById(R.id.EditText10);
mEditText_1 = (EditText) findViewById(R.id.EditText_1);
mEditText_2 = (EditText) findViewById(R.id.EditText_2);
mEditText_3 = (EditText) findViewById(R.id.EditText_3);
mEditText_4 = (EditText) findViewById(R.id.EditText_4);
mEditText_5 = (EditText) findViewById(R.id.EditText_5);
mEditText_6 = (EditText) findViewById(R.id.EditText_6);
mEditText_7 = (EditText) findViewById(R.id.EditText_7);
mEditText_8 = (EditText) findViewById(R.id.EditText_8);
mEditText_9 = (EditText) findViewById(R.id.EditText_9);
mEditText_10 = (EditText) findViewById(R.id.EditText_10);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handle click
TextView textGemiddelde = (TextView)
findViewById(R.id.Gemiddelde);
textGemiddelde.append(calculate());
}
});
}
public double sum() {
sum = 0;
counter = 0;
double cijfer1 = Double.valueOf(mEditText1.getText().toString());
double cijfer2 = Double.valueOf(mEditText2.getText().toString());
double cijfer3 = Double.valueOf(mEditText3.getText().toString());
double cijfer4 = Double.valueOf(mEditText4.getText().toString());
double cijfer5 = Double.valueOf(mEditText5.getText().toString());
double cijfer6 = Double.valueOf(mEditText6.getText().toString());
double cijfer7 = Double.valueOf(mEditText7.getText().toString());
double cijfer8 = Double.valueOf(mEditText8.getText().toString());
double cijfer9 = Double.valueOf(mEditText9.getText().toString());
double cijfer10 = Double.valueOf(mEditText10.getText().toString());
double weging1 = Double.valueOf(mEditText_1.getText().toString());
double weging2 = Double.valueOf(mEditText_2.getText().toString());
double weging3 = Double.valueOf(mEditText_3.getText().toString());
double weging4 = Double.valueOf(mEditText_4.getText().toString());
double weging5 = Double.valueOf(mEditText_5.getText().toString());
double weging6 = Double.valueOf(mEditText_6.getText().toString());
double weging7 = Double.valueOf(mEditText_7.getText().toString());
double weging8 = Double.valueOf(mEditText_8.getText().toString());
double weging9 = Double.valueOf(mEditText_9.getText().toString());
double weging10 = Double.valueOf(mEditText_10.getText().toString());
if (cijfer1 != 0 && weging1 != 0) {
this.sum += cijfer1 * weging1;
this.counter++;
}
if (cijfer2 != 0 && weging2 != 0) {
this.sum += cijfer2 * weging2;
this.counter++;
}
if (cijfer3 != 0 && weging3 != 0) {
this.sum += cijfer3 * weging3;
this.counter++;
}
if (cijfer4 != 0 && weging4 != 0) {
this.sum += cijfer4 * weging4;
this.counter++;
}
if (cijfer5 != 0 && weging5 != 0) {
this.sum += cijfer5 * weging5;
this.counter++;
}
if (cijfer6 != 0 && weging6 != 0) {
this.sum += cijfer6 * weging6;
this.counter++;
}
if (cijfer7 != 0 && weging7 != 0) {
this.sum += cijfer7 * weging7;
this.counter++;
}
if (cijfer8 != 0 && weging8 != 0) {
this.sum += cijfer8 * weging8;
this.counter++;
}
if (cijfer9 != 0 && weging9 != 0) {
this.sum += cijfer9 * weging9;
this.counter++;
}
if (cijfer10 != 0 && weging10 != 0) {
this.sum += cijfer10 * weging10;
this.counter++;
}
return sum;
}
public String calculate() {
sum = 0;
counter = 0;
sum();
if (counter != 0)
value = String.valueOf(sum / counter);
else
value = "Invalid Input";
return value;
}
}