这是我的代码:
import static java.lang.System.*;
public class Triples
{
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)
{
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
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(a%2<=1 && b%2<=1 && c%2<=1)
{
return 1;
}
}
}
}
}
}
}
return 1;
}
public String toString()
{
String output="";
output = greatestCommonFactor(a, b, c);
return output+"\n";
}
}
我需要让它打印出变量 a、b 和 c,但我不知道如何让它这样做。我目前收到的错误消息是“a 无法解析为变量 b 无法解析为变量 c 无法解析为变量”
如果有帮助,这里是相关实验室表的链接: https ://docs.google.com/open?id= 0B_ifaCiEZgtcX08tbW1jNThZZmM
更新 这是我更新的 toString 方法:
public String toString()
{
int a = 0;
int b = 0;
int c = 0;
String output="";
output += greatestCommonFactor(a, b , c) + "\n";
return output;
}
在我编辑时,我最伟大的CommonFactor 方法:
private int greatestCommonFactor(int a, int b, int c)
{
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
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(a%2<=1 && b%2<=1 && c%2<=1)
{
return greatestCommonFactor(a, b, c);
}
}
}
}
}
}
}
//return 1;
}
更新#2
这是(希望)为 bestCommonFactor 和 toString 方法编写代码的更正确方法:
private int greatestCommonFactor(int a, int b, int c)
{
a = 0;
b = 0;
c = 0;
for(int n = 0; n <= number; n++)
{
int max = number;
for(a = 1; a <= max; a++)
{
a = n;
for(b = a +1; b <= max; b++)
{
b =n;
for(c = b + 1; c <= max; c++)
{
c = n;
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(a%2<=1 && b%2<=1 && c%2<=1)
{
return a;
}
}
}
}
}
}
}
return greatestCommonFactor(a, b, c);
}
public String toString()
{
String output="";
output += greatestCommonFactor(a, b , c) + "\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 trip = new Triples( big);
//call the toString method to print the triangle
System.out.println( trip );
System.out.print("Do you want to enter more data? ");
choice=keyboard.next();
}while(choice.equals("Y")||choice.equals("y"));
}
}