Java 菜单驱动程序以检查正数、负数或奇数偶数

Java 菜单驱动程序以检查正数、负数或奇数偶数

如果一个数字大于0,则它被称为正数,如果小于0,则称为负数。负数以减号(-)作为标识。

如果一个数字可以被2整除,则称为偶数,否则称为奇数。在这篇文章中,我们将看到如何使用Java编程语言来检查一个数字是正数、负数还是偶数、奇数。我们将使用switch case实现这个应用程序。

为了向您展示一些示例

实例-1

Suppose the input number is 12.
Checking it as a positive or negative number, it will result 12 as a positive number as it is greater than 0.

实例-2

Suppose the input number is -12.
Checking it as a positive or negative number, it will result -12 as a negative number as it is less than 0.

实例-3

Suppose the input number is 2022.
Checking it as an even or odd number, it will result 2022 as an even number as it is divisible by 2.

实例-4

Suppose the input number is 7.
Checking it as an even or odd number, it will result 7 as an odd number as it is not divisible by 2.

语法

要执行诸如检查一个数字是正数还是负数,或者它是偶数还是奇数这样的操作,我们使用带有一些基本逻辑的“if else语句” 。

以下是 “if else语句” 的语法。

if(condition) {
   //code to be executed
}

步骤

步骤-1 - 要求用户输入所需的数字。

步骤-2 - 显示菜单。

步骤-3 - 要求用户输入选择。

步骤-4 - 使用switch case根据选择执行操作。

步骤-5 - 打印结果。

让我们看一个程序来更清楚地理解它。

示例

import java.util.*;
public class Main{
   public static void main(String args[]) {
      int num;
      Scanner inn = new Scanner(System.in);
      mainLoop: while (true) {
         System.out.println("\n***Menu***");
         System.out.println("1. Check if a Number is Positive or Negative");
         System.out.println("2. Check Whether a Number is Even or Odd");
         System.out.println("3. Terminate the program");
         System.out.println("Enter action number (1-3): ");
         int command;
         if (inn.hasNextInt()){
            command = inn.nextInt();
         }else {
            System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.");
            inn.nextLine();
            continue;
         }

         switch(command) {
            case 1:
            System.out.print("Enter a number: ");
            num = inn.nextInt();
            if(num>0) {
               System.out.println("Entered number "+num+" is positive.");
            }
            //checks the number is less than 0 or not
            else if(num<0) {
               System.out.println("Entered number "+num+" is negative.");
            }
            else {
               System.out.println("The number is zero.");
            }
            break;
            case 2:
            System.out.print("Enter a number: ");
            num = inn.nextInt();
            if(num % 2 == 0) {
               System.out.println("Entered number "+num+" is even.");
            }
            else {
               System.out.println("Entered number "+num+" is odd.");
            }
            break;
            case 3:
            System.out.println("Program terminated");
            break mainLoop;
            default:
            System.out.println("Wrong choice!!");
         }
      }
   }
}

输出

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
1
Enter a number: 45
Entered number 45 is positive.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
2
Enter a number: 45
Entered number 45 is odd.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
1
Enter a number: -456
Entered number -456 is negative.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
2
Enter a number: 2022
Entered number 2022 is even.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
x
ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
5
Wrong choice!!

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
3
Program terminated

在这篇文章中,我们通过使用菜单驱动的方法来探讨了如何在Java中检查一个数字是正数、负数还是偶数、奇数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程