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


如果一个数大于0,则称其为正数;如果小于0,则称其为负数。负数前面带有减号(-)以标识其为负数。

如果一个数能被2整除,则称其为偶数;否则,称其为奇数。在本文中,我们将学习如何使用Java编程语言来检查一个数是正数、负数、偶数还是奇数。我们将使用switch case语句来实现该应用程序。

问题陈述

编写一个Java程序来检查正数、负数或奇偶数。

输入1

Suppose the input number is 12.

输出1

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.

输出2

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.

输出3

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.

输出4

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 }

检查正数、负数或奇偶数的步骤

以下是检查正数、负数或奇偶数的步骤:

  • 要求用户输入所需的数字。
  • 显示菜单。
  • 要求用户输入他们的选择。
  • 使用switch case语句跳转到选择并执行操作。
  • 打印结果。

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

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

让我们来看一下Java程序以便更清楚地理解它:

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中检查一个数是正数、负数、偶数还是奇数。

更新于:2024年8月9日

浏览量:1000+

开启您的职业生涯

通过完成课程获得认证

开始学习
广告