2 回答
TA贡献1963条经验 获得超6个赞
public class MainActivity extends AppCompatActivity {
private EditText ch1,ch2,ch3,ch4,ch5;
private EditText g1,g2,g3,g4,g5;
private Button gpa_calc;
private TextView result;
Double final =0;
//private int credit_sum = 0;
//private int grade_sum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Credit Hours
ch1 = (EditText)findViewById(R.id.C_H1);
ch2 = (EditText)findViewById(R.id.C_H2);
ch3 = (EditText)findViewById(R.id.C_H3);
ch4 = (EditText)findViewById(R.id.C_H4);
ch5 = (EditText)findViewById(R.id.C_H5);
//Grades
g1= (EditText)findViewById(R.id.L_G1);
g2= (EditText)findViewById(R.id.L_G2);
g3= (EditText)findViewById(R.id.L_G3);
g4= (EditText)findViewById(R.id.L_G4);
g5= (EditText)findViewById(R.id.L_G5);
//Button
gpa_calc = (Button) findViewById(R.id.button);
//Result
result = (TextView)findViewById(R.id.result);
gpa_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final= calculateGPA();
result.setText(final.toString());
}
});
}
public double calculateGPA() {
double quality_points = 0;
double credit_sum = 0;
double gpa=0;
double chour1 = Double.valueOf(ch1.getText().toString());
double chour2 = Double.valueOf(ch2.getText().toString());
double chour3 = Double.valueOf(ch3.getText().toString());
double chour4 = Double.valueOf(ch4.getText().toString());
double chour5 = Double.valueOf(ch5.getText().toString());
String l1 = String.valueOf(g1.getText());
String l2 = String.valueOf(g2.getText());
String l3 = String.valueOf(g3.getText());
String l4 = String.valueOf(g4.getText());
String l5 = String.valueOf(g5.getText());
double[] creditArray = new double[]{chour1, chour2, chour3, chour4, chour5};
String[] gradeArray = new String[]{l1,l2,l3,l4,l5};
credit_sum = chour1+chour2+chour3+chour4+chour5;
for(int i=0; i<gradeArray.length; i++){
if (gradeArray[i].equals("A")){
quality_points += (creditArray[i] * 4);
}
else if (gradeArray[i].equals("B")){
quality_points += (creditArray[i] * 3);
}
else if (gradeArray[i].equals("C")){
quality_points += (creditArray[i] * 2);
}
else if (gradeArray[i].equals("D")){
quality_points += (creditArray[i] * 1);
}
else if (gradeArray[i].equals("F")){
quality_points += (creditArray[i] * 0);
}
}
gpa = (quality_points/credit_sum);
String str = Double.toString(gpa);
return gpa;
}}
TA贡献1829条经验 获得超7个赞
这可能与您的问题无关,但我修复了您的布局。当然,您仍然可以改进它。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/L_G5"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/Grade"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/C_H5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H5"
tools:targetApi="o" />
<EditText
android:id="@+id/L_G4"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/Grade"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/C_H4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H4"
tools:targetApi="o" />
<EditText
android:id="@+id/C_H5"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:ems="10"
android:hint="@string/credit_hours"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/c5"
app:layout_constraintStart_toEndOf="@+id/c5"
tools:targetApi="o" />
<TextView
android:id="@+id/c5"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c4" />
<TextView
android:id="@+id/c3"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c2" />
<EditText
android:id="@+id/C_H4"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/credit_hours"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/c4"
app:layout_constraintTop_toBottomOf="@+id/C_H3"
android:importantForAutofill="no" tools:targetApi="o" />
<TextView
android:id="@+id/c4"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c3" />
<EditText
android:id="@+id/L_G3"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/Grade"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/C_H3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H3"
tools:targetApi="o" />
<EditText
android:id="@+id/C_H3"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:ems="10"
android:hint="@string/credit_hours"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/c3"
app:layout_constraintStart_toEndOf="@+id/c3"
tools:targetApi="o" />
<EditText
android:id="@+id/L_G2"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/Grade"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/C_H2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H2"
tools:targetApi="o" />
<EditText
android:id="@+id/C_H2"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:ems="10"
android:hint="@string/credit_hours"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/c2"
app:layout_constraintStart_toEndOf="@+id/c2"
tools:targetApi="o" />
<TextView
android:id="@+id/c2"
android:layout_width="70dp"
android:layout_height="21dp"
android:layout_marginStart="24dp"
android:layout_marginTop="50dp"
android:text="@string/course_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/c1" />
<TextView
android:id="@+id/c1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="112dp"
android:text="@string/course_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/C_H1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:ems="10"
android:hint="@string/credit_hours"
android:importantForAutofill="no"
android:inputType="numberDecimal"
app:layout_constraintBaseline_toBaselineOf="@+id/c1"
app:layout_constraintStart_toEndOf="@+id/c1"
tools:targetApi="o" />
<EditText
android:id="@+id/L_G1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/Grade"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/C_H1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/C_H1"
tools:targetApi="o" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="78dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="@string/compute_gpa"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/C_H5" />
<TextView
android:id="@+id/result"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_marginStart="170dp"
android:layout_marginTop="46dp"
android:layout_marginEnd="175dp"
android:layout_marginBottom="119dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>
添加回答
举报