[英]How to use a boolean return value from my subprogram in my main
Hey I made a subprogram which returns either true or false. 嘿,我做了一个子程序,它返回true或false。 Now in my main I am not able to use it in my if statement.
现在在主目录中,我无法在if语句中使用它。 This is how I shaped my if statement:
这就是我塑造if语句的方式:
Uniqueyear(year);
if (Uniqueyear()) {
System.out.println(year + "is unique.");
}
else {
year++;
Uniqueyear(year);
}
}
However, when i compile my program it says " method Uniqueyear in class DistinctDigits cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length " 但是,当我编译程序时,它说“类DistinctDigits中的方法Uniqueyear无法应用于给定类型;必需:int发现:无参数原因:实际参数和形式参数列表的长度不同”
I am new to programming and cant seem to fix it.I also tried writing " if (Uniqueyear(year)){.. but it messes up my output. This is my subprogram in case there is some problem in my return statement or anything else: 我是编程新手,似乎无法解决它。我还尝试编写“ if(Uniqueyear(year)){..,但它弄乱了我的输出。这是我的子程序,以防万一我的return语句中有问题其他:
public static boolean Uniqueyear(int a) {
String year= Integer.toString(a);
for (int i=0; i<year.length(); i++) {
for(int j = i + 1; j < year.length(); j ++){
if (year.charAt(i) == year.charAt(j)) {
return true;
}
}
}
return false;
Any help will be appreciated. 任何帮助将不胜感激。 :)
:)
The "subprogram" is called a method. “子程序”称为方法。 Since it is static it is a class method (a method that is called on a class instead of on an instance, like non-static methods).
由于它是静态的,所以它是一个类方法(一种在类上而不是在实例上调用的方法,例如非静态方法)。
When you call Uniqueyear(year), you are calling the Uniqueyear method in that class. 当您调用Uniqueyear(year)时,您正在该类中调用Uniqueyear方法。
You have defined one Uniqueyear method that receives an int. 您已经定义了一个接收整数的Uniqueyear方法。 So whenever you call it, the compiler will expect to find an integer value passed to it.
因此,无论何时调用它,编译器都会期望找到传递给它的整数值。
Therefore, when you do 因此,当您这样做
if (Uniqueyear()) {
}
the compiler complains that you haven't passed the required parameters to Uniqueyear. 编译器抱怨您没有将必需的参数传递给Uniqueyear。
You could store the return value of your previous call to Uniqueyear and then use the if on that value. 您可以存储上次对Uniqueyear的调用的返回值,然后在该值上使用if。
boolean myBoolean = Uniqueyear(year);
if (myBoolean) {
}
Or you could also, alternatively, remove the first Uniqueyear call and put it directly inside of the if condition. 或者,您也可以删除第一个Uniqueyear调用,并将其直接放在if条件内。
if (Uniqueyear(year)) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.