简体   繁体   English

如何在主程序中使用子程序的布尔返回值

[英]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.

相关问题 如何将“主活动”中的布尔值传递给片段? - How do I pass a boolean value from my Main Activity to a fragment? 尽管我的布尔方法正确返回true或false,但是我不能在main中使用return - Though my boolean method returns true or false correctly, I can't use the return in main 为什么我的主方法中的返回值不包含值? - Why is my return value not holding a value in my main method? 如何从Action Listener类中的For循环中获取值(int)以在主类中使用? - How to get the value(int) from a For Loop inside a Action Listener class to use in my main class? 如何从子类到主类使用方法? - How do I use my methods from my sub class into my main one? 如何在java中返回我的主面板? - how to return to my main panel in java? 如何让我的子菜单返回主菜单 - How to make my submenu return to Main Menu 如何从包含随机值的单独 class 调用方法并使用该值在我的主 class 中打印? - How can I call a method from a separate class that contains a randomized value and use that value to print in my main class? 为什么默认的布尔值会影响我的测试结果 - Why does the default Boolean return value influence my tests outcome 如何让程序返回主菜单? - How to have my program return to the main menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM