当前位置: 首页 > 知识库问答 >
问题:

在条件句中使用布尔true和false

杜茂
2023-03-14
public class FavNum {

    public static void isEven(int x){
       boolean b;
       if (x%2==0) {
          b=true;
       }else{
          b=false;
       }
    }

    public static void isSingle(int x){
       boolean b;
       if (x > 0 && x < 10){
          b=true;        
       }else{
          b=false;
          JOptionPane.showMessageDialog(null, "Your favorite number is not a single digit!");        
       }
    }

    public static void all(int x){
       isEven (x);
       //What should I add here to use isEven and isSingle in conditionals? 
       //For example, I want to say something like if isEven and isSingle are true, then say this. 
       //Or if isEven is true and isSingle is not, say this.  But I don't know how to properly write those conditionals.
    }

    public static void main(String[] args) {
       int x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?"));        
    }    
}

这方面的指示如下”

  • 创建名为FavoriteNumber的新项目

到目前为止,我已经完成了前4项任务。我在第五颗子弹上遇到了麻烦(我想我还不完全理解)。我试图通过创建一个名为all的新方法来实现这一点,我计划在其中调用isSingle和isEven,并使用if-else语句来比较它们并相应地返回消息。但我被卡住了,在我上面的代码中留下了解释我的问题的注释。

有人能帮我完成这个任务吗?或者至少给我指出正确的方向?

谢谢你!

共有3个答案

蔚学林
2023-03-14

看起来你已经弄清楚了算法部分从你的方法中,你需要返回布尔值而不是无效

 public static boolean isEven(int x){
   return (x % 2) == 0;
 }
阎作人
2023-03-14
public class Joption {

    // declared outside so easier to be accessed by other methods in the class
    private static int x;

    // is the number passed into this method even?
    private static boolean isEven(int x) {
        if (x % 2 == 0) {
            return true;
        }
        return false;
    }

    // is the number passed into this method single?
    private static boolean isSingle(int x) {
        if (x >= 0 || x < 10) {
            return true;
        }
        return false;
    }

    // is the number passed in even AND single?
    // notice how the number passed in flows into the two methods you just created. Their returns help you determine if they are even AND single.
    private static void isEvenAndSingle(int x) {
        if (isEven(x) && isSingle(x)) {
            JOptionPane.showMessageDialog(null, x + "is Even and Single!");
        }
    }

    // same as above method but now it's checking to see if it's even and NOT single.
    private static void isEvenAndNotSingle(int x) {
        if (isEven(x) && !(isSingle(x))) {
            JOptionPane.showMessageDialog(null, x + "is Even but NOT single!");
        }
    }

    // your main running method
    // here you actually need to call the isEvenAndSingle() and isEvenAndNotSingle() methods with the retrieved x value from the JOptionPane so that you can actually print the appropriate messages in the responding JOptionPane
    public static void main(String[] args) {
        x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?"));
        isEvenAndSingle(x);
        isEvenAndNotSingle(x);
    }

}

如前所述,您的问题在于您从JOptionPane中检索值的方式。您将其作为int获取。但您将其作为布尔值传递到您的方法中。您实际上需要做的是将值作为int传递(请参阅is0022()和isSing()方法)并让它们返回一个布尔值。

然后要求您创建与您刚刚创建的两个方法的比较。一个用于如果数字是偶数和单个,另一个用于如果数字是偶数和非单个。

我用注释对代码进行了注释,以帮助您更好地理解!祝你未来的java编码好运!:)

董奇思
2023-03-14

你在比较布尔值和数字:

boolean x;

 if (x%2==0) {   //Error here
    b=true;
}

 if (x > 0 && x < 10){     //Error here
       b=true;

你不能那样做。

如果x是truefalse,则无法检查其是否大于0,也无法对其进行算术运算。

 类似资料:
  • 我有一个布尔语句,使用

  • 问题内容: 现在,我知道如何检查数据框中多列中的特定值。但是,我似乎无法弄清楚如何基于布尔响应执行if语句。 例如: 使用和遍历目录并将特定文件读入数据框。 现在检查跨多个列的数据框。第一个值是列名(column1),下一个值是我在该列(香蕉)中寻找的特定值。然后,我正在检查另一列(column2)的特定值(绿色)。如果这两个都是正确的,我想执行一项特定的任务。但是,如果它是错误的,我想做其他事情

  • 这个页面的操作符可用于根据条件发射或变换Observables,或者对它们做布尔运算: 条件操作符 amb( ) — 给定多个Observable,只让第一个发射数据的Observable发射全部数据 defaultIfEmpty( ) — 发射来自原始Observable的数据,如果原始Observable没有发射数据,就发射一个默认数据 (rxjava-computation-expressi

  • 问题内容: 使用标准而不是测试特定值是否是标准约定。 如果要确定一个值是否准确(不仅仅是一个真值),是否有使用而不是使用任何理由?这在CPython(2.x和3.x),Jython,PyPy等实现之间是否有所不同? 示例:say用作您要与value或其他真值相区别的单例值: 是否存在使用会产生与的不同结果的情况? 注意:我知道Python布尔值-如果x :,则x如果x == True,则x如果x为

  • All 判定是否Observable发射的所有数据都满足某个条件 传递一个谓词函数给All操作符,这个函数接受原始Observable发射的数据,根据计算返回一个布尔值。All返回一个只发射一个单个布尔值的Observable,如果原始Observable正常终止并且每一项数据都满足条件,就返回true;如果原始Observable的任何一项数据不满足条件就返回False。 RxJava将这个操作

  • 我发现了这个语法: 这个有两个点的语法是什么:叫? 在哪里可以找到有关它的信息? 它是仅适用于布尔值还是以其他不同的方式实现?