读取数据文件

我正在我的 AP java 课上编写一个程序,我很困惑。该程序应该使用另一个名为 EasyReader 的程序读取数据文件。该程序由一个对象类组成,用于查找最大数、中等数和最小数。Test 类应该读取给出一组数字的数据文件 (numbers.bat),并且该程序应该打印数据文件所有行中的最大、中和最小行。但是,它只读取数据文件的一行(中间一行)而不是全部三行。它正在显示

Largest = 7.3 Medium = 5 Smallest = 3.2

请帮忙。谢谢你!

这是对象类:

public class Numbers 

{


    double small;

    double medium;

    double large;


    public Numbers(double A, double B, double C)

    {


      if(A>=B && A>=C)

      {

        large = A;



        if(B>=C && B<=A)

        {

          medium = B;


          small = C;

        }

        else

        {

          medium = C;

          small = B;

        }


      } 

      else if(B>=A && B>=C)

      {

          large = B;



            if(A>=C && A<=B)

            {

              medium = A;


              small = C;

            }

            else

            {

              medium = C;

              small = A;

            }



      } 


      else 

      {

          large = C;



            if(A>=B && A<=C)

            {

              medium = A;


              small = B;

            }

            else

            {

              medium = B;

              small = A;

            }


      }


    }




        public double large()

        {

            return large;


        }

        public double medium()

        {

            return medium;


        }

        public double small()

        {

            return small;


        }

    }   

最后是数据文件(numbers.bat)

4 9 2.5
3.2 5 7.3
12 8.2 9.1

现在的输出:

Largest = 7.3 Medium = 5.0 Smallest = 3.2

预期输出:

Largest = 12.0 Medium = 7.3 Smallest = 2.5


慕神8447489
浏览 96回答 1
1回答

明月笑刀无情

仅当您的 value1 (A) 大于或等于其他两个时,您的数字构造函数才会执行任何操作。如果不是,则会将小、中和大保留为 0。编辑:在这里扩展答案,因为它看起来更干净。所以在你的数字构造函数中public Numbers(double A, double B, double C){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(A>=B && A>=C)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; large = A;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(B>=C && B<=A)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; medium = B;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; small = C;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; medium = C;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; small = B;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }只有一个主 if 语句检查 A 是否最大,其余的都嵌套在其中。您还需要一个 if 语句来判断 B 或 C 是否最大。public Numbers(double A, double B, double C){&nbsp; if(A>=B && A>=C)&nbsp; {&nbsp; &nbsp; large = A;&nbsp; &nbsp; if(B>=C && B<=A)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; medium = B;&nbsp; &nbsp; &nbsp; small = C;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; medium = C;&nbsp; &nbsp; &nbsp; small = B;&nbsp; &nbsp; }&nbsp; } else if if(B>=A && B>=C)&nbsp; {&nbsp; &nbsp; //B is the largest, add code to determine medium and small as you did before&nbsp; } else {&nbsp; &nbsp; //C is the largest, add code to determine medium and small as you did before&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java