用于将输入的整数确定为奇数或甚至是Java的应用程序

时间:2017-11-21 17:13:23

标签: java

我是java语言的新手,刚刚学习了基本的东西。我必须编写一个应用程序,要求用户输入一个整数,然后显示一个语句,指示整数是偶数还是奇数。这就是我所做的:

import java.util.Scanner;

public class EvenOdd
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer >> ");
      int num = input.nextInt();

      double divisibleByTwo = num % 2;

      if(divisibleByTwo == 0)
         System.out.println("The integer entered is even.");
      else
         System.out.println("The integer entered is odd.");   
   }
}

上面的代码工作正常,但是对我的代码进行分级的软件要求我的代码采用以下格式:

import java.util.Scanner;
class EvenOdd
{
   public static void main(String[] args) 
   {
      // accept user input and check if number is even or odd
   }
   public static boolean isEven(int number)
   {
      // check if number is even
   }
}

以下是我的设置,但我有几个错误,我不确定如何开始并让它正常工作。任何帮助将不胜感激。

import java.util.Scanner;
class EvenOdd
{
   public static void main(String[] args) 
   {
      // accept user input and check if number is even or odd
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer >> ");
      int num = input.nextInt();

      isEven(num);

      boolean divisible;
      isDivisibleByTwo = divisible;

      if(divisible == true)
         System.out.println("The integer entered is even.");
      else
         System.out.println("The integer entered is odd.");   
   }
   public static boolean isEven(int number)
   {
      // check if number is even
      int remainderByTwo = number % 2;
      boolean isDivisbleByTwo = (remainderByTwo == 0);
      return isDivisibleByTwo; 
   }
}

4 个答案:

答案 0 :(得分:3)

在第一个示例中,当您执行

时正确分配变量
with open(filename,'ab') as outfile:

然后使用返回的值检查

double divisibleByTwo = num % 2;

返回true或false。但是,在下一个代码示例中,您可以

if(divisibleByTwo == 0)...

由于你永远不会为isEven返回的值赋予divisible,所以if语句总是返回false,因为boolean变量的默认值为false。

您需要分配isEven方法调用返回的值,然后像这样使用该值

isEven(num); //NO asignment!!!!

boolean divisible; //defaults to false
isDivisibleByTwo = divisible;

if(divisible == true) //divisble will always be false

或者您可以使用isEven方法的返回值,而不必将其分配给这样的变量

boolean isDivisibleByTwo = isEven(num);

if(isDivisibleByTwo)
   System.out.println("The integer entered is even.");
else
   System.out.println("The integer entered is odd.");

答案 1 :(得分:1)

您不需要过度复杂化。您只需要更简洁的代码:

public class OddEven {

    public static void main(String[] args) {
        // accept user input and check if number is even or odd
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer >> ");
        int num = input.nextInt();

        boolean remainder = isEven(num);
        if (remainder == true)
            System.out.println("The integer entered is even.");
        else
            System.out.println("The integer entered is odd.");
    }

    public static boolean isEven(int number) {

        if (number % 2 == 0)
            return true;
        else
            return false;
    }

}

答案 2 :(得分:0)

您的目标是在isEven方法中进行所有计算,您的主方法将仅用于从用户获取数字并调用其他方法。无需在那里进行计算。

换句话说,如下所示:

import java.util.Scanner;
class EvenOdd
{
   public static void main(String[] args) 
   {
      // Get the user input
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an integer >> ");
      int num = input.nextInt();
      // Check if the input is correct ?

      // Call your method and check the return value
      if(isEven(num))
         System.out.println("The integer entered is even.");
      else
         System.out.println("The integer entered is odd.");   
   }

   // And then your method is good
   public static boolean isEven(int number)
   {
      // check if number is even
      return (number % 2) == 0;
   }
}

答案 3 :(得分:0)

编写一个应用程序,该应用程序读取两个整数,并确定并打印第一个整数是否是第二个整数的倍数。例如,如果用户输入15和3,则第一个数字是第二个的倍数。如果用户输入2和4,则第一个数字不是第二个的倍数。