首頁 > Java > java教程 > Java選單驅動程式以檢查數字類型

Java選單驅動程式以檢查數字類型

WBOY
發布: 2023-08-29 15:33:08
轉載
628 人瀏覽過

Java選單驅動程式以檢查數字類型

在Java中,我們有原始的數值資料類型,用來表示短整數、位元組型、整數、長整數型、浮點型和雙精確度浮點型。這些數值資料類型允許我們根據它們的範圍來表示不同的整數和實數,因為每個特定的資料類型都有它的下限和上限。

Below are the 6 primitive numeric data types 

byte: Range -128 to 127 (Size 1 byte)
short: Range -32,768 to 32,767 (Size 2 bytes)
int: Range -2,147,483,648 to 2,147,483,647 (Size 4 bytes)
long: Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (8 bytes)
float: Range 6 to 7 decimal digits for Fractional Numbers (Size 4 bytes)
double: Range 15 decimal digits for Fractional Numbers (Size 8 bytes)
登入後複製

在本文中,我們將看到如何透過使用Java程式語言來檢查數字的類型。我們將使用switch case來實作該應用程式。

展示一些實例給你看 

Instance-1

Suppose we have taken an integer type of value i.e. 222222 then it must show “number entered is a valid integer”.
登入後複製

實例-2

Suppose we have taken a double type of value i.e 120.0 then it must show “number entered is a valid double”.
登入後複製

Instance-3

的中文翻譯為:

實例-3

Suppose we have taken a float type of value i.e 2.1 then it must show “number entered is a valid float”.
登入後複製

Instance-4

Suppose we have taken a byte type of value i.e 23 then it must show “number entered is a valid byte”.
登入後複製

Instance-5

的翻譯為:

實例-5

#
Suppose we have taken a short type of value i.e 32,765 then it must show “number entered is a valid short”.
登入後複製

Instance-6

Suppose we have taken a long type of value i.e 222222222222 then it must show “number entered is a valid long”.
登入後複製

Syntax

To check the type of number we will check its datatype. If the datatype matches then the result will be positive otherwise negative. To check it we have a function called hasNextData_Type.

以下是檢查「整數資料型別」的語法:

hasNextInt()
登入後複製

Following is the syntax to check “double datatype” −

hasNextDouble()
登入後複製

以下是檢查「浮點數資料型別」的語法:

hasNextFloat()
登入後複製

Following is the syntax to check “byte datatype” −

hasNextByte()
登入後複製

以下是檢查「short資料類型」的語法 -

hasNextShort()
登入後複製

Following is the syntax to check “long datatype” −

hasNextLong()
登入後複製

Algorithm

Step-1 − Ask the user to enter their choice.

第二步 - 顯示選單。

步驟-3 − 要求使用者輸入資料。

第四步驟 - 使用switch case語句進行選擇並執行操作。

步驟-5 - 列印結果。

Let’s see the program to understand it clearly.

Example

的中文翻譯為:

範例

import java.util.*;
public class Main{
   public static void main(String args[]){
      mainLoop: while (true) {
         Scanner sc = new Scanner( System.in );
         System.out.println("\n***Menu***");
         System.out.println("1. Check for Integer");
         System.out.println("2. Check for Double");
         System.out.println("3. Check for Float");
         System.out.println("4. Check for Byte");
         System.out.println("5. Check for Short");
         System.out.println("6. Check for Long");
         System.out.println("7. Terminate the program");
         System.out.println("Enter action number (1-7): ");
         int command = sc.nextInt();
         switch(command) {
            case 1:
            int input = 0;
            System.out.println("Enter an integer for its validation");
            if(sc.hasNextInt()) {
               input = sc.nextInt();
               System.out.println(input+" is a valid Integer");
            }
            else{
               System.out.println("It is not a valid Integer");
            }
            break;
            case 2:
            double input1 = 0;
            System.out.println("Enter a Double for its validation");
            if(sc.hasNextDouble()) {
               input1 = sc.nextDouble();
               System.out.println(input1+" is a valid Double");
            }
            else {
               System.out.println("It is not a valid Double");
            }
            break;
            case 3:
            float input2;
            System.out.println("Enter a Float for its validation");
            if(sc.hasNextFloat()) {
               input2 = sc.nextFloat();
               System.out.println(input2+" is a valid Float");
            }
            else{
               System.out.println("It is not a valid Float");
            }
            break;
            case 4:
            byte input3 = 0;
            System.out.println("Enter a Byte for its validation");
            if(sc.hasNextByte()) {
               input3 = sc.nextByte();
               System.out.println(input3+" is a valid Byte");
            }
            else{
               System.out.println("It is not a valid Byte");
            }
            break;
            case 5:
            short input4 = 0;
            System.out.println("Enter a Short for its validation");
            if(sc.hasNextShort()) {
               input4 = sc.nextShort();
               System.out.println(input4+" is a valid Short");
            }
            else{
               System.out.println("It is not a valid Short");
            }

            break;
            case 6:
            long input5 = 0;
               System.out.println("Enter a Short for its validation");
               if(sc.hasNextLong()) {
                  input5 = sc.nextLong();
                  System.out.println(input5+" is a valid Long");
               }
               else{
                  System.out.println("It is not a valid Long");
               }
               break;
               case 7:
               System.out.println("Program terminated");
               break mainLoop;
               default:
               System.out.println("Wrong choice!!");
         }
      }
   }
}
登入後複製

Output

#
***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
1
Enter an integer for its validation
22222
22222 is a valid Integer

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
2
Enter a Double for its validation
2.4
2.4 is a valid Double

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
3
Enter a Float for its validation
2.5
2.5 is a valid Float

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
4
Enter a Byte for its validation
123
123 is a valid Byte

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
5
Enter a Short for its validation
1234
1234 is a valid Short

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
6
Enter a Short for its validation
345
345 is a valid Long

***Menu***
1. Check for Integer
2. Check for Double
3. Check for Float
4. Check for Byte
5. Check for Short
6. Check for Long
7. Terminate the program
Enter action number (1-7):
7
Program terminated
登入後複製

在本文中,我們探討如何透過使用選單驅動方法來檢查Java中的數字類型。

以上是Java選單驅動程式以檢查數字類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板