[英]limiting user input for hexadecimal numbers
Basically the User needs to input a Hexadecimal number which then converts it to decimal and binary numbers. 基本上,用户需要输入一个十六进制数字,然后将其转换为十进制和二进制数字。 I created two separate classes for the decimal and binary conversion and they work perfectly.
我为十进制和二进制转换创建了两个单独的类,它们可以完美地工作。 What I wanted to do is to limit the input number making "90" the minimum input and "FF" the maximum one.
我想做的是限制输入数,使“ 90”为最小输入,“ FF”为最大输入。
public static void main(String[] args) {
ForwardorBack Binary = new ForwardorBack();
Speed Decimal = new Speed();
String Hexadecimal = "";
Scanner input = new Scanner(System.in);
System.out.println("Enter Hexadecimal Number");
Hexadecimal = input.nextLine();
while (Hexadecimal.length() > 2 || Hexadecimal.length() < 2) {
System.out.println("Error Enter different number:");
Hexadecimal = input.nextLine();
}
Decimal.wheels(Hexadecimal);
Binary.move(Hexadecimal);
}
Here is a method to use in the head of the while loop 这是在while循环的开头使用的方法
public static boolean validateInput(String input) {
if (input.length != 2) {
return false;
}
//Replace with own conversion if needed
try {
int decimal = Integer.parseInt(hex, 16);
if (decimal >= 90 && decimal <= 255) {
return true;
}
} catch (NumberFormatException e) {
return false;
}
}
Then use it like so 然后像这样使用它
while(!validateInput(hexadecimal)) {
System.out.println("Error Enter different number:");
hexadecimal = input.nextLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.