Java菜单驱动程序以检查数字类型

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菜单驱动程序以检查数字类型的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


应用场景 C端用户提交工单、工单创建完成之后、会发布一条工单创建完成的消息事件(异步消息)、MQ消费者收到消息之后、会通知各处理器处理该消息、各处理器处理完后都会发布一条将该工单写入搜索引擎的消息、最终该工单出现在搜索引擎、被工单处理人检索和处理。 事故异常体现 1、异常体现 从工单的流转记录发现、
线程类,设置有一个公共资源 package cn.org.chris.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Descrip
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量
Java 9中JShell中的不同启动脚本是什么?
使用Java的位填充错误检测技术
java中string是什么
如何使用Java中的JSON-lib API将Map转换为JSON对象?
Java菜单驱动程序以检查数字类型
使用Junit的Maven项目 - 检查银行账号
JAVA编程基础
在Java中使用throw、catch和instanceof来处理异常
在Java中,将数组分割为基于给定查询的子数组后,找到子数组的最大子数组和
如何在Java中从给定的字符串中删除HTML标签?
在PHP中,IntlChar getBlockCode()函数的翻译如下:
如何在Android中实现按下返回键再次退出的功能?
如何使用Java中的流式API解析JSON字符串?
Java中的模式类