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語句跳轉到選擇並執行操作。
- 列印結果。
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中檢查一個數是正數、負數、偶數還是奇數。
廣告