Java 如何使用参数调用私有方法
我很难在toString方法中获得一个带参数的私有方法,但我不知道如何让这两个方法协同工作 主要类别:Java 如何使用参数调用私有方法,java,methods,private-methods,Java,Methods,Private Methods,我很难在toString方法中获得一个带参数的私有方法,但我不知道如何让这两个方法协同工作 主要类别: 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) {
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)
{
int max = number;
for(int n = 1; n <= max; n++)
{
for(a = n; 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)
{
String last = a + "" + b + c;
}
}
}
}
}
}
}
return 1;
}
public String toString()
{
String output="";
output = output + this.greatestCommonFactor( ) + " \n";
return output;
}
}
如果你发现你需要对这个实验室进行澄清,这里有一个谷歌的实验室工作表文档:嗯,是的。您没有将任何内容传递给
greatestCommonFactor
。我不确定当您没有向方法传递足够的参数时,toString()
方法会发生什么。是的。您没有将任何内容传递给greatestCommonFactor
。我不确定当您没有向方法传递足够的参数时,toString()
方法会发生什么情况。您需要像
output = output + this.greatestCommonFactor(1,2,3) + " \n";
问题是,除非您将参数传递给toString,否则此代码似乎非常有限。或者,您需要在类上设置一些字段,其中包含将传递到函数中的内容。您需要像
output = output + this.greatestCommonFactor(1,2,3) + " \n";
问题是,除非您将参数传递给toString,否则此代码似乎非常有限。或者,您需要在类上设置一些字段,其中包含将传递到函数中的内容。变量
a
,b
和c
可以在此处用作局部变量。这将允许您从greatestCommonFactor
的参数列表中删除它们:
private int greatestCommonFactor() {
int a = 0;
int b = 0;
int c = 0;
...
由于它们仅在方法范围内需要。变量
a
,b
&c
可在此处用作局部变量。这将允许您从greatestCommonFactor
的参数列表中删除它们:
private int greatestCommonFactor() {
int a = 0;
int b = 0;
int c = 0;
...
因为它们只在方法的范围内需要。那么我应该如何使用变量,使a、b和c都可以成为某种东西(在本例中),那么我应该如何使用变量,使a、b和c都可以成为某种东西(在本例中),无论出于什么原因,实验室最初的设置方式,它将整数作为参数而不是标题事实上,它们应该声明为局部变量,因为您不需要从外部源获取这些变量的初始值。@tech_geek23是否实现了greatestCommonFactor()方法的主体,或者这是提供给您的?也许您的实现不是预期的。它最初的方式是
private int greatestCommonFactor(int a,int b,int c){int max=number;return 1;}
格式正确,当然,当它确定3个数字的GCF实际上是1时,我如何携带a,b,c到toString方法?不管出于什么原因,实验室最初的设置方式是,它将INT作为参数而不是主键。事实上,它们应该声明为局部变量,因为您不需要从外部源获取这些变量的初始值。@tech_geek23您实现了greatestCommonFactor()方法的主体吗?或者这是给您的?也许您的实现不是预期的。它最初的方式是private int greatestCommonFactor(int a,int b,int c){int max=number;return 1;}
格式正确,当然,当它确定3个数字的GCF实际上是1时,我如何携带a,b,我发现有趣的是,您的私有方法名为“greatestCommonFactor()”,但它似乎没有计算给定三个数字的GCF。你确定你正确地完成了这个任务吗?这是一个很好的观点,实验表上的“算法帮助”有这样的内容:>循环a>>循环b>>>循环c>>>>检查a、b、c是否满足毕达哥拉斯的三重标准我想你是在尝试用“最伟大的公因子()”方法实现整个算法。我也认为这不是最好的地方,也不是实验室想要的地方。在你的代码中,该算法的实现属于哪里?因此,如果整个算法不一定属于greatestCommonFactor(),那么各个部分可能需要实现在哪里?如果我在做这个任务,我将在Lab11
中实现另一个main可以调用的静态方法。我发现有趣的是,您的私有方法名为“greatestCommonFactor()”,但它似乎没有计算给定三个数字的GCF。你确定你正确地完成了这个任务吗?这是一个很好的观点,实验表上的“算法帮助”有这样的内容:>循环a>>循环b>>>循环c>>>>检查a、b、c是否满足毕达哥拉斯的三重标准我想你是在尝试用“最伟大的公因子()”方法实现整个算法。我也认为这不是最好的地方,也不是实验室想要的地方。在你的代码中,该算法的实现属于哪里?因此,如果整个算法不一定属于greatestCommonFactor(),那么各个部分可能需要实现在哪里?如果我在做这个任务,我将在Lab11
中实现另一个静态方法,main可以调用该方法。