import java.util.Scanner;
public class NumberConversionSystems {
public static void main(String[] args) {
//String binary = toBinaryString(number);
Scanner input = new Scanner(System.in);
System.out.println("Number Conversion Systems n");
// Display the menu
System.out.println("1.t Decimal to Binary");
System.out.println("2.t Decimal to Hexadecimal");
System.out.println("3.t Binary to Decimal");
System.out.println("4.t Hexadecimal to Decimal n");
System.out.println("Your choice?");
//Get user's choice
int choice = input.nextInt();
switch (choice) {
case 1: System.out.println("nEnter Decimal Number");
break;
case 2: System.out.println("nEnter Decimal Number");
break;
case 3: System.out.println("nEnter Binary");
break;
case 4: System.out.println("nEnter Hexadecimal");
break;
default:
System.out.println("nInvalid choice. Please choose a number between 1 and 4.");
choice = input.nextInt();
break;
}
if (choice == 1) {
int number = input.nextInt();
String binary = toBinaryString(number);
binary = recursive(number);
System.out.printf("Decimal to Binary (%d) = %s", number, binary);
}
else if (choice == 2) {
int number2 = input.nextInt();
String hexadecimal = toHexString(number2);
hexadecimal = recursiveDecHex(number2);
System.out.printf("Decimal to Hexadecimal (%d) = %s ", number2, hexadecimal);
}
else if (choice == 3 ) {
String binary2 = input.next();
int decimal = toDecimalUsingParseInt(binary2);
decimal = recursiveBin(binary2);
System.out.printf("n2. Binary to decimal - recursive(%s) = %d ", binary2, decimal);
}
else {
String hex = input.next();
int decimal = toHexUsingParseInt(hex);
decimal = recursiveHexDec(hex);
System.out.printf("Hexadecimal to Decimal (%s) = %d ", hex, decimal);
}
input.close();
}
private static String toBinaryString(int number) {
return Integer.toBinaryString(number);
}
private static String toHexString(int number) {
return Integer.toHexString(number);
}
private static int toDecimalUsingParseInt(String binaryNumber) {
return Integer.parseInt(binaryNumber, 2);
}
private static int toHexUsingParseInt(String number) {
return Integer.parseInt(number, 16);
}
private static String recursive(int number) {
StringBuilder builder = new StringBuilder();
if (number > 0) {
String binaryNumber = recursive(number / 2);
int digit = number % 2;
builder.append(binaryNumber + digit);
}
return builder.toString();
}
private static String recursiveDecHex(int number) {
StringBuilder builder = new StringBuilder();
if (number > 0) {
String hexNumber = recursiveDecHex(number / 16);
String hexCode = "0123456789ABCDEF";
int hexDigit = number % 16;
builder.append(hexNumber + hexCode.charAt(hexDigit));
}
return builder.toString();
}
private static int recursiveBin(String binaryNumber) {
int decimal = 0;
int length = binaryNumber.length();
if (length > 0) {
String substring = binaryNumber.substring(1);
int digit = Character.getNumericValue(binaryNumber.charAt(0));
decimal = digit * (int) Math.pow(2, length - 1) + recursiveBin(substring);
}
return decimal;
}
private static int recursiveHexDec(String hexNumber) {
int decimal = 0;
String hexCode = "0123456789ABCDEF";
hexNumber = hexNumber.toUpperCase();
int length = hexNumber.length();
if (length > 0) {
char ch = hexNumber.charAt(0);
int digit = hexCode.indexOf(ch);
String substring = hexNumber.substring(1);
decimal = digit * (int) Math.pow(16, length - 1) + recursiveHexDec(substring);
}
return decimal;
}
}
当我选择一个无效的数字(不在1和4之间的数字)时,程序将显示"无效的选择。请选择一个介于 1 和 4 之间的数字,当我在此之后输入有效数字时,程序将停止运行。
例如,如果我选择"1",它不会要求我输入十进制数。我错过了什么?
您没有循环,您一次又一次地尝试它,直到输入一个有效的数字。
试试这个:
int choice = -1;
while (choice < 0 || choice > 4) {
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("nEnter Decimal Number");
break;
case 2:
System.out.println("nEnter Decimal Number");
break;
case 3:
System.out.println("nEnter Binary");
break;
case 4:
System.out.println("nEnter Hexadecimal");
break;
default:
System.out.println("nInvalid choice. Please choose a number between 1 and 4.");
break;
}
}
但请记住,输入正确数字旁边的循环无法逃脱。
您正在等待交换机默认值中的新条目。 所以它只是坐在那里,直到你把一个放进去。
您可以在切换之前更轻松地完成该部分:
boolean badEntry = true;
do{
System.out.println("Your choice?");
int choice = input.nextInt();
if (( choice<1 )|| (choice>4)) {
System.out.println("nInvalid choice. Please choose a number between 1 and 4.");
} else {
badEntry = false;
}
}
while (badEntry);
您的程序不会停止。 它实际上是在您输入有效数字后等待输入。 如果您不相信我,请在输入有效选项后尝试输入另一个数字。 对我来说,它的工作原理是这样的。
Number Conversion Systems
1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Hexadecimal to Decimal
Your choice?
5
Invalid choice. Please choose a number between 1 and 4.
1
23
Decimal to Binary (23) = 10111
但是,当您到达程序中输入有效选项的点时,打印出消息(如"输入十进制数")的部分已经过去;这就是您没有收到该消息的原因。