Java 在使用了一组3个不同的数字后,我如何修改程序以继续查找适用的数字?

Java 在使用了一组3个不同的数字后,我如何修改程序以继续查找适用的数字?,java,class,Java,Class,我的问题是,即使它正确地打印出值和所有内容,我仍然需要它来测试更高的值,显然仍然低于我的样本输入的最大值,即110 这是我的密码: import static java.lang.System.*; public class Triples { private int first; private int second; private int third; private int number; public Triples() { //this(0);

我的问题是,即使它正确地打印出值和所有内容,我仍然需要它来测试更高的值,显然仍然低于我的样本输入的最大值,即110

这是我的密码:

import static java.lang.System.*;

public class Triples
{
  private int first;
  private int second;
  private int third;
  private int number;

  public Triples()
  {
    //this(0);
  }

  public Triples(int num)
  {
    number = num;
  }

  public void setNum(int num)
  {
    number = num;
  }

  private int greatestCommonFactor(int a, int b, int c)
  {
    int g;
    int h;

    if(a<b && a<c)
      g = a;
    else if(b< a && b<c)
      g = b;
    else
      g = c;

    for(int i = g; i > 0; i--)
    {
      if((a%i == 0) && (b%i == 0))
      {
        h = i;
        for(int j = i; j>0; j--)
        {
          if((h%j==0) && (c%j == 0))
          {
            return j;
          }
        }
      }
    }
  return -1;
  }


  public String check4Triples()
  {
    int max = number;
    String amIdoneYet;
    //int a;
    //int b;
    //int c;
    for(int n = 1; n <= max; n++)
      //{

      for(int a = n; a <= max; a++)
      {
        first = a;
        for(int b = a +1; b <= max; b++)
        {
          second =b;
          for(int c = b + 1; c <= max; c++)
          {
            third = c;
            if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
            {
              if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
              {
                if(this.greatestCommonFactor(a, b, c)== 1)
                {
                  amIdoneYet = "";
                  amIdoneYet += a + " " + b + " "+ c;
                  return amIdoneYet;
                }
              }
            }                   
          }
        }   
      }
      return null;
    }

    public String toString()
    {
      String output=" ";
      output += check4Triples() + " \n";

      return output;
    }
  }
我的输出应该是这样的:

  • 3 4 5
  • 51213
  • 72425
  • 81517
  • 94041
  • 116061
  • 123537
  • 138485
  • 166365
  • 20 21 29
  • 2099101
  • 28 45 53
  • 335665
  • 367785
  • 398089
  • 48 55 73
  • 6091109
  • 65 72 97
更新
经过仔细考虑之后,我认为问题在于for循环无法知道是否要再次运行。。。我仍然认为这个问题与我的跑步课有关。但是在我的
check4Triples
方法中,我基本上只告诉它找到三个最低的适用数字。我不知道如何告诉它也检查其他值,问题的关键是,在找到GCF为1时立即返回一个字符串值,而您试图做的是循环到最大值并找到所有三元组:

if (this.greatestCommonFactor(a, b, c) == 1) {
    return "" + a + " " + b + " "+ c;
}
第二个问题是围绕错误代码的循环有一个额外的循环,
1..n
,该循环已由
a=1..n
处理:

for (int n = 1; n <= max; n++) {
    for (int a = n; a <= max; a++) {
        ...
我相信这更接近你的意图。我也可以使用短路逻辑:

if (isPythagoreanTriple(a, b, c)
        && (mod2(a, b) || mod2(b, a))
        && (greatestCommonFactor(a, b, c) == 1)) {
    ret += "" + a + " " + b + " " + c + "\n";
}

我觉得这段代码有点难读,尤其是有垂直和水平的空格。我不确定你看到了多少空格,但我打算写它的方式是为了减少拼凑,如果有人认为有必要的话,看起来更干净,我不介意你是否想修改代码以减少空白。。。。我只是看不到任何空白的选择让我有点困惑;虽然我非常反对在newline上使用大括号,但在我看来,这并不是一个问题,而不是在关键字和操作符周围加空格。此外,深层嵌套结构也很难推理:代码的侧面并不是一个显示它有多棒的图形,代码几乎不可能被读取。你应该使用更具描述性的变量名,而不是a、b、c、d、g等。比如,int numberToFactor、factor1、factor2等。不仅如此,你的嵌套for循环中还有i和j,这给了读者7个总共一个字母的int变量来跟踪。我不确定我是否理解这行
if(ispythagoreantree(a,b,c))是怎么写的
works@tech_geek23那是因为我不会给你一个完全有效的程序。逻辑和你现在的一样,只是更具沟通性。您不需要在自己的代码中更改这一行,这只会使它更容易理解。我还添加了
mod2
min
功能,以防止我的头部受伤。好的,我根据您指出的问题调整了我的代码,现在它似乎已经完全正常工作了。主要问题是我的
return amIdoneYet
行的位置吗?@tech_geek23正如我所说,你的问题的关键是这个,以及额外的循环。
public String check4Triples() {
    int max = number;
    String ret = "";

    for (int a = 1; a <= max; a++) {
        for (int b = a + 1; b <= max; b++) {
            for (int c = b + 1; c <= max; c++) {
                if (isPythagoreanTriple(a, b, c)) {
                    if (mod2(a, b) || mod2(b, a)) {
                        if (greatestCommonFactor(a, b, c) == 1) {
                            ret += "" + a + " " + b + " " + c + "\n";
                        }
                    }
                }
            }
        }
    }

    return ret;
}
3 4 5
5 12 13
7 24 25
8 15 17
9 40 41
11 60 61
12 35 37
13 84 85
16 63 65
20 21 29
28 45 53
33 56 65
36 77 85
39 80 89
48 55 73
65 72 97
if (isPythagoreanTriple(a, b, c)
        && (mod2(a, b) || mod2(b, a))
        && (greatestCommonFactor(a, b, c) == 1)) {
    ret += "" + a + " " + b + " " + c + "\n";
}