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-1suppose we have taken an integer type of value i.e. 222222 then it must show “number entered is a valid integer”.
实例-2suppose we have taken a double type of value i.e 120.0 then it must show “number entered is a valid double”.
instance-3的中文翻译为:实例-3suppose we have taken a float type of value i.e 2.1 then it must show “number entered is a valid float”.
instance-4suppose we have taken a byte type of value i.e 23 then it must show “number entered is a valid byte”.
instance-5的翻译为:实例-5suppose we have taken a short type of value i.e 32,765 then it must show “number entered is a valid short”.
instance-6suppose we have taken a long type of value i.e 222222222222 then it must show “number entered is a valid long”.
syntaxto 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()
algorithmstep-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 integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):1enter an integer for its validation2222222222 is a valid integer***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):2enter a double for its validation2.42.4 is a valid double***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):3enter a float for its validation2.52.5 is a valid float***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):4enter a byte for its validation123123 is a valid byte***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):5enter a short for its validation12341234 is a valid short***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):6enter a short for its validation345345 is a valid long***menu***1. check for integer2. check for double3. check for float4. check for byte5. check for short6. check for long7. terminate the programenter action number (1-7):7program terminated
在本文中,我们探讨了如何通过使用菜单驱动方法来检查java中的数字类型。
以上就是java菜单驱动程序以检查数字类型的详细内容。
