我的问题是,即使它正确打印出值和所有内容,我仍然需要它来测试更高的值,显然仍然小于我的样本输入的最大值,即 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;
}
}
但是,我相信手头的问题在于我的跑步者课程:
import static java.lang.System.*;
import java.util.Scanner;
public class Lab11j
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
String choice="";
do{
out.print("Enter the max number to use : ");
int big = keyboard.nextInt();
//instantiate a TriangleThree object
Triples triple = new Triples(big);
//call the toString method to print the triple
out.println( triple );
System.out.print("Do you want to enter more data? ");
choice=keyboard.next();
}while(choice.equals("Y")||choice.equals("y"));
}
}
这是我的输出应该是这样的:
- 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
- 20 99 101
- 28 45 53
- 33 56 65
- 36 77 85
- 39 80 89
- 48 55 73
- 60 91 109
- 65 72 97
更新
在考虑了一些之后,我认为问题在于 for 循环无法知道是否再去......我仍然认为这个问题与我的跑步者类有关。但在我的check4Triples
方法中,我基本上只告诉它找到三个最低的适用数字。我不知道如何告诉它也检查其他值