我在我的AP Java类中编写一个程序,我很困惑。该程序应该使用另一个名为EasyReader的程序读取数据文件。该程序由一个Object类组成,该类查找最大数量,中等数量和最小数量。 Test类应该读取给出一组数字的数据文件(numbers.bat),程序应该打印出数据文件所有行中的最大,中和最小的行。但是,它仅读取数据文件的一行(中间一行),而不是全部三行。它正在显示
Largest = 7.3 Medium = 5 Smallest = 3.2
请帮忙。谢谢!

这是Object类:

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;

        }
    }

这是Test类:
public class NumbersTest
{

    public static void main(String[] args)
    {

        EasyReader file = new EasyReader("numbers.dat");
        double value1 = file.readDouble();
        double value2 = file.readDouble();
        double value3 = file.readDouble();


        value1 = file.readDouble();
        value2 = file.readDouble();
        value3 = file.readDouble();




         Numbers nums = new Numbers(value1, value2, value3);

         System.out.println("Largest = " + nums.large()  + " Medium = " + nums.medium() + " Smallest = " + nums.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

最佳答案

您的数字构造函数仅在value1(A)大于或等于其他两个时才执行任何操作。如果不是,它将在0处保留小,中和大值。

编辑:在这里扩大答案,因为它看起来更干净。
所以在你的数字构造器中

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;
            }

        }

    }

您只有一个主要的if语句来检查A是否最大,其余的嵌套在其中。您还需要if语句来确定B或C是否最大。
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 if(B>=A && B>=C)
  {
    //B is the largest, add code to determine medium and small as you did before
  } else {
    //C is the largest, add code to determine medium and small as you did before
  }

}

关于java - 读取数据文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58635169/

10-09 02:09