繁体   English   中英

输入一个整数,它是负数,是偶数还是正数和奇数

[英]Enter an integer that is negative and even or positive and odd

我正在创建一个程序,要求用户提供几个整数,并且其中一个语句要求用户“输入一个整数,该整数应为负数,偶数或正数和奇数”。 我将如何确切地提出这样一个问题? 到目前为止,我已经知道了。 我不知道应该怎么做,因为我的指示非常令人困惑。 这是我的问题所在:

4.向用户询问一个整数,该整数可以是负数,偶数或正数和奇数。 使用if语句和复合条件。

import java.util.Scanner;

public class ExerciseFour   
{
public static void main ( String[] argsv )
{


    int choice;
    int choiceTwo;
    int choiceThree;

    Scanner input = new Scanner(System.in);

        System.out.println( "Enter a number between 0 and 10" );
        choice = input.nextInt();


            if ( choice > 0 && choice < 10 ) 
            {   System.out.println( "Valid number" );   
            }
            else 
            {   System.out.println( "Invalid number" );
            return;
            }




        System.out.println( "Enter a number divisible by 2 or 3?" );
        choiceTwo = input.nextInt();

            if ( choiceTwo % 2 == 0 && choiceTwo % 3 == 0 )
            {   System.out.println( "Valid number" );
            }
            else    
            {   System.out.println( "Number not divisible by 2 or 3" );
                return;             
            }

        System.out.println( "Enter an integer that is negative and even or positive and odd (Ex. -2 or 7 )" );
        choiceThree = input.nextInt();

            if ( choiceThree  ) 
            { 
            } 
            else 
            { 
            }
((choiceThree > 0) && (choiceThree % 2 == 1)) || ((choiceThree < 0) && (choiceThree % 2 == 0))

上面是您要查找的复合条件,这意味着:

(
  (choiceThree > 0)      //positive number / greater than zero
   &&                    // AND
  (choiceThree % 2 == 1) //odd number: (an odd number divided by two has a remainder of 1)
)
||                       // OR
(
 (choiceThree < 0)       //negative number / less than zero
  &&
 (choiceThree % 2 == 0)  //even number (an even number divided by two has a remainder of 0)
)

编辑: %模运算符
a % b的结果是整数除法a / b的余数。

关键是使用模运算符 偶数可被2除尽,没有余数。 所以:

if (choiceThree  < 0) {
    if (choiceThree % 2 == 0) {
        System.out.println ("Valid");
    } else {
        System.out.println ("Invalid");
    }
} else {
    if (choiceThree % 2 != 0) {
        System.out.println ("Valid");
    } else {
        System.out.println ("Invalid");
    }
}

当然,这有点麻烦。 表示布尔逻辑的一种更优雅的方法是使用Exclusive或(xor)运算符 如果只有一个操作数的结果为true则此运算符返回true

if (choiceThree  > 0 ^ choiceThree % 2 == 0) {
    System.out.println ("Valid");
} else {
    System.out.println ("Invalid");
}

创建一种在以下情况之一下返回true的方法:

public boolean isCorrectInteger(int number){

    if ((number < 0) && (number % 2 == 0)) { //negative and even
        return true;
    } else if((number < 0) && (number % 2 == 1)) { // positive and odd
        return true;
    } else { // other cases
        return false;
    }
}

这可以在一个更大的条件下编写,为了简单的示例,我将其分为两个部分。

还应考虑到当前没有将零赋给正数或负数-您可以使用<=>=运算符随意更改它。

我认为第二个问题有误

choiceTwo % 2 == 0 && choiceTwo % 3 == 0

您可能想写|| 而不是&&您伤心到2 OR 3 ;-)

对于另一个问题:您有两个布尔表达式,可能是正确的:

向用户询问一个整数,该整数可以是负数甚至是偶数

(choiceThree < 0 && choiceThree % 2 == 0)

或正数或奇数。

(choiceThree > 0 && choiceThree   % 2 == 1)

使用if语句和复合条件。

因此,只需将它们与具有逻辑OR(||)的语句组合即可

尝试这个:

 System.out.println( "Enter an integer that is negative and even or positive and odd (Ex. -2 or 7 )" );
 choiceThree = input.nextInt();
 if ( (choiceThree>0 && choiceThree%2==1) || (choiceThree<0 && choiceThree%2==0) ) 
 { 
     System.out.println("Correct");
 } 
 else 
 { 
     System.out.printlnt("ERROR");
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM