当我单击一个按钮时,结果值不断变化。同样有时候结果是错误的

时间:2018-07-06 11:43:55

标签: android android-studio button android-spinner

我写了一个代码,用于根据成绩计算GPA。但是问题是每次我单击按钮时,它都会更新数组中的值,并且结果会不断变化。在阅读了很多文章之后,我真的不知道该如何克服这个问题。还有其他方法可以实现而不会出现此问题。请让我知道你们。希望我能尽快得到答案。预先感谢大家。

public class CSE4SEM extends AppCompatActivity {
    static Float gpa = 0.0F;
    final int FinalPoints[] = new int[9];
    final int rcvdPoints[] = new int[9];
    TextView tvres;
    Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9;
    Button btn;
    int sumRes = 0, sumPoints = 0;
    int maxPoints[] = new int[]{4, 4, 4, 3, 3, 3, 2, 2, 2};
    int arrear = 0;

    public void calc(View v) {
        for (int j = 0; j < maxPoints.length; j++) {
            FinalPoints[j] = rcvdPoints[j] * maxPoints[j];
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cse4_sem);

        tvres = findViewById(R.id.tvRes);
        btn = findViewById(R.id.btn);
        sp1 = findViewById(R.id.sp1);
        sp2 = findViewById(R.id.sp2);
        sp3 = findViewById(R.id.sp3);
        sp4 = findViewById(R.id.sp4);
        sp5 = findViewById(R.id.sp5);
        sp6 = findViewById(R.id.sp6);
        sp7 = findViewById(R.id.sp7);
        sp8 = findViewById(R.id.sp8);
        sp9 = findViewById(R.id.sp9);


        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp1.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 10;
                }
                if (sp1.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 9;
                }
                if (sp1.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 8;
                }
                if (sp1.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 7;
                }
                if (sp1.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 6;
                }
                if (sp1.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 5;
                }
                if (sp1.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[0] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp2.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 10;
                }
                if (sp2.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 9;
                }
                if (sp2.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 8;
                }
                if (sp2.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 7;
                }
                if (sp2.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 6;
                }
                if (sp2.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 5;
                }
                if (sp2.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[1] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp3.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 10;
                }
                if (sp3.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 9;
                }
                if (sp3.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 8;
                }
                if (sp3.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 7;
                }
                if (sp3.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 6;
                }
                if (sp3.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 5;
                }
                if (sp3.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[2] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        sp4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp4.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 10;
                }
                if (sp4.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 9;
                }
                if (sp4.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 8;
                }
                if (sp4.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 7;
                }
                if (sp4.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 6;
                }
                if (sp4.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[3] = 5;
                }
                if (sp4.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        sp5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp5.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 10;
                }
                if (sp5.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 9;
                }
                if (sp5.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 8;
                }
                if (sp5.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 7;
                }
                if (sp5.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 6;
                }
                if (sp5.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 5;
                }
                if (sp5.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[4] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        sp6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp6.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 10;
                }
                if (sp6.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 9;
                }
                if (sp6.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 8;
                }
                if (sp6.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 7;
                }
                if (sp6.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 6;
                }
                if (sp6.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 5;
                }
                if (sp6.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getAp q       plicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[5] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        sp7.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp7.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 10;
                }
                if (sp7.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 9;
                }
                if (sp7.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 8;
                }
                if (sp7.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 7;
                }
                if (sp7.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 6;
                }
                if (sp7.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 5;
                }
                if (sp7.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[6] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        sp8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp8.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 10;
                }
                if (sp8.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 9;
                }
                if (sp8.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 8;
                }
                if (sp8.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 7;
                }
                if (sp8.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 6;
                }
                if (sp8.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 5;
                }
                if (sp8.getSelectedItemPosition() == 6) {
                    // Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[7] = 0;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        sp9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (sp9.getSelectedItemPosition() == 0) {
                    //Toast.makeText(getApplicationContext(),"0 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 10;
                }
                if (sp9.getSelectedItemPosition() == 1) {
                    //Toast.makeText(getApplicationContext(),"1 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 9;
                }
                if (sp9.getSelectedItemPosition() == 2) {
                    //Toast.makeText(getApplicationContext(),"2 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 8;
                }
                if (sp9.getSelectedItemPosition() == 3) {
                    //Toast.makeText(getApplicationContext(),"3 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 7;
                }
                if (sp9.getSelectedItemPosition() == 4) {
                    // Toast.makeText(getApplicationContext(),"4 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 6;
                }
                if (sp9.getSelectedItemPosition() == 5) {
                    //Toast.makeText(getBaseContext(),"5 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 5;
                }
                if (sp9.getSelectedItemPosition() == 6) {
                    //Toast.makeText(getApplicationContext(),"6 String Selected",Toast.LENGTH_LONG).show();
                    rcvdPoints[8] = 0;
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


           /* for(int i=0;i<maxPoints.length;i++) {
                FinalPoints[i]=rcvdPoints[i]*maxPoints[i];
            }*/

                calc(view);

                for (int num : FinalPoints) {
                    sumPoints = num + sumPoints;
                }

                for (int num : maxPoints) {
                    sumRes = num + sumRes;
                }
                float sumPointsFloat = sumPoints, sumResFloat = sumRes;
                gpa = sumPointsFloat / sumResFloat;
                tvres.setText(Float.toString(gpa));
             }
        });
    }
}

0 个答案:

没有答案