调用 Java 类方法来处理 EditTexts 的值

我对 EditTexts 值的问题已解决(将数据从片段传递到另一个片段),所以现在我终于可以继续使用这些值了。但是除了我还有另一个问题,还有谁呢?我没有,但我有一些。


所以,我的目标是从用户那里获得 5 个值,并且我想使用这个值(计算平均值)。


正如我在上一个主题中提到的,我有一个 Java 类,它有很多方法。其中一种方法是成绩计算器。


我的普通 Java 类的一部分:


package com.example.afotel;


import java.util.ArrayList;


public class Average {


        double numberOfGradeOne, numberOfGradeTwo, numberOfGradeThree, numberOfGradeFour, numberOfGradeFive;

        private double numberOfGrades, gradeSum, gradeAverage;


         void calculateAverage (double numberOfGradeFive, double numberOfGradeFour, double numberOfGradeThree, double numberOfGradeTwo, double numberOfGradeOne) {

            numberOfGrades=numberOfGradeFive+numberOfGradeFour+numberOfGradeThree+numberOfGradeTwo+numberOfGradeOne;

            gardeSum=5*numberOfGradeFive+4*numberOfGradeFour+3*numberOfGradeThree+2*numberOfGradeTwo+numberOfGradeOne;

            gradeAverage=gradeSum/numberOfGrades;

        }


public double getAverage () {

    return gradeAverage;

    }

在我的第二个片段(jegyekFelLe(请注意它不是英语))中,我想调用 Java 类的成绩计算器方法。


    package com.example.afotel;



    import android.os.Bundle;

    import android.support.annotation.Nullable;

    import android.support.v4.app.Fragment;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.EditText;

    import android.widget.TextView;


    public class jegyekFelLe extends Fragment {



        public jegyekFelLe() {

        }

          @Nullable

            @Override

public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,

                         @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_jegyek_fel_le, container, false);


    Bundle bundle = getArguments();

        }

    }

如您所见,我想,如果我先调用 calculateAverage 方法,那么我可以得到结果,但它不起作用。我对此有所期待。解决办法是什么?我对此无能为力。什么或/以及我应该如何解决问题,使用 5 个输入和 calculateAverage 并获得最终值以显示在第二个片段中?



浮云间
浏览 107回答 2
2回答

白衣非少年

尝试这个 :public class Average {    double numberOfGradeOne, numberOfGradeTwo, numberOfGradeThree, numberOfGradeFour, numberOfGradeFive;     double numberOfGrades, gradeSum, gradeAverage;    public Average(double numberOfGradeOne, double numberOfGradeTwo, double numberOfGradeThree, double numberOfGradeFour, double numberOfGradeFive, double numberOfGrades) {        this.numberOfGradeOne = numberOfGradeOne;        this.numberOfGradeTwo = numberOfGradeTwo;        this.numberOfGradeThree = numberOfGradeThree;        this.numberOfGradeFour = numberOfGradeFour;        this.numberOfGradeFive = numberOfGradeFive;        this.numberOfGrades = numberOfGrades;        calculateAverage();    }    void calculateAverage () {        numberOfGrades=numberOfGradeFive+numberOfGradeFour+numberOfGradeThree+numberOfGradeTwo+numberOfGradeOne;        gradeSum =5*numberOfGradeFive+4*numberOfGradeFour+3*numberOfGradeThree+2*numberOfGradeTwo+numberOfGradeOne;        gradeAverage=gradeSum/numberOfGrades;    }    public double getGradeAverage() {        return gradeAverage;    }}在你的片段中:average = new Average(12,34,54,12,44.6,5);        Log.v("Average",""+average.getGradeAverage());        textView.setText(average.getGradeAverage()+" ");或者你甚至可以评论 calculateAverage(); 从构造函数并像这样调用:average = new Average(12,34,54,12,44.6,5);        average.calculateAverage();        Log.v("Average",""+average.getGradeAverage());        textView.setText(average.getGradeAverage()+" ");

不负相思意

您应该尝试通过调用来使用static methodthen 调用Average.calculate(gradeOne, gradeTwo, gradeThree, gradeFour, gradeFive)。class应该看起来像这样。import java.util.Arrays;import java.util.OptionalDouble;import java.util.concurrent.atomic.AtomicReference;public class Average {public static double calculate(double... inputs) {&nbsp; &nbsp; AtomicReference<Double> multiplier = new AtomicReference<>((double) 0);&nbsp; &nbsp; OptionalDouble average = Arrays.stream(inputs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(value -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiplier.set((multiplier.get() + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value * multiplier.get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .average();&nbsp; &nbsp; return average.isPresent() ? average.getAsDouble() : 0;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java