【问题标题】:using boolean true and false in conditionals在条件句中使用布尔值 true 和 false
【发布时间】:2017-02-20 16:54:39
【问题描述】:
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 的新项目
  • 编写一个名为 isEven 的布尔方法,该方法接受一个整数参数并检测整数是否为偶数,返回真或假。
  • 编写第二个名为 isSingleDigit 的布尔方法,它接受一个整数参数并检测整数是否为单个数字,返回真或假
  • 在 main 中,提示用户输入他们喜欢的号码。
  • 用一个条件语句测试数字,如果是偶数和单个数字,则打印一条消息(有创意)。
  • 如果都是奇数且不是单个数字,则使用另一个条件进行测试,然后打印另一个(创意)消息。
  • 添加一个条件来测试奇数或单个数字并打印创意消息。
  • 添加一个最终条件,测试是否为偶数而不是单个数字,并打印一条最终创意消息"

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

有人可以帮我完成这项任务吗?或者至少为我指明正确的方向?

谢谢!

【问题讨论】:

  • x 已经是一个布尔值
  • 我该如何解决这个问题?这是我上课的第一周,所以我还不是很有经验。
  • @LaurenMcCabe 我认为您将函数的返回类型与函数采用的参数混淆了。函数 isOff 可能应该将一个数字作为参数并返回一个布尔值。你的接受一个布尔值作为参数并且什么都不返回。
  • @LaurenMcCabe 请不要通过稍后编辑来更改问题。这将使所有答案变得毫无意义。如果你真的有一个新问题,那就再问一个新问题!
  • @911DidBush 对此我很抱歉。我必须等待 90 分钟才能制作一个新的,所以我认为只更改这个更有效。抱歉给大家带来了困惑!

标签: java boolean


【解决方案1】:

您正在将布尔值与数字进行比较:

boolean x;

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

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

你不能那样做。

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

【讨论】:

    【解决方案2】:
    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 传递(参见 isEven() 和 isSingle() 方法)并让它们返回布尔值。

    然后,系统会要求您与刚刚创建的两种方法进行比较。一个表示偶数且单数,一个表示偶数且非单数。

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

    【讨论】:

    • 别担心! :D 希望你有更多有趣的编码!如果您需要更多解释,请给我留言!
    【解决方案3】:

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

     public static boolean isEven(int x){
       return (x % 2) == 0;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2012-09-12
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多