用户以十六进制或二进制形式输入一个数字(如果没有给出错误消息,则必须以0x或0b作为前缀)。将用户输入读入为字符串
我不能用基数,我只是用integer.parseInt(s)
我不知道如何使这个计划奏效。帮帮忙我在这里少了什么?
import java.util.Scanner;
public class conversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//ask user to enter either hex or binary
System.out.println("Please enter a hex (0x) or binary (0b) number: ");
//read input as string
String hexString = input.nextLine();
String binString = input.nextLine();
//if prefix (0x)
if ((hexString.substring(0,2)).equals("0x")) {
int hex = Integer.parseInt(hexString, 16);
System.out.println("Converting from base-16 to base-10.\n" + hex);
//if there are no digits or invalid digits after Ox
//if ((hexString.substring(beginIndex))) {
// System.out.println("Error parsing base-16 number");
//}
//if prefix (0b)
} else if ((binString.substring(0,2)).equals("0b")) {
int bin = Integer.parseInt(binString, 2);
System.out.println("Converting from base-2 to base-10.\n" + bin);
//if there are no digits or invalid digits after Ob
//if ((binString.substring(beginIndex))) {
// System.out.println("Error parsing base-2 number");
//}
} else {
System.out.println("I don't know how to covert that number");
}
发布于 2015-02-17 07:13:09
因为您试图转换整个字符串,包括0x或0b,所以会出现错误。在转换之前,您需要从字符串中删除它。以下是完整的工作示例:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//ask user to enter either hex or binary
System.out.println("Please enter a hex (0x) or binary (0b) number: ");
//read input as string
String hexString = input.nextLine();
String binString = input.nextLine();
//if prefix (0x)
if (hexString.startsWith("0x")) {
hexString = hexString.replaceAll("0x", "");
int hex = Integer.parseInt(hexString, 16);
System.out.println("Converting from base-16 to base-10.\n" + hex);
//if there are no digits or invalid digits after Ox
//if ((hexString.substring(beginIndex))) {
// System.out.println("Error parsing base-16 number");
//}
//if prefix (0b)
} else if (binString.startsWith("0b")) {
binString = binString.replaceAll("0x", "");
int bin = Integer.parseInt(binString, 2);
System.out.println("Converting from base-2 to base-10.\n" + bin);
//if there are no digits or invalid digits after Ob
//if ((binString.substring(beginIndex))) {
// System.out.println("Error parsing base-2 number");
//}
} else {
System.out.println("I don't know how to covert that number");
}
}
因此,为了优化您的代码,您不需要像这里这样两次使用input.nextLine();
:
//read input as string
String hexString = input.nextLine();
String binString = input.nextLine();
但是可以使用单个字符串输入,如: //read输入作为字符串consoleInput = input.nextLine();
//if prefix (0x)
if (consoleInput.startsWith("0x")) {
consoleInput = consoleInput.replaceAll("0x", "");
// ...
} else if (consoleInput.startsWith("0b")) {
consoleInput = consoleInput.replaceAll("0x", "");
int bin = Integer.parseInt(consoleInput, 2);
// ...
} else {
System.out.println("I don't know how to covert that number");
}
如果由于某种原因,用户输入无法转换,我很高兴添加“尝试捕捉”。下面是一个有用的例子:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//ask user to enter either hex or binary
System.out.println("Please enter a hex (0x) or binary (0b) number: ");
//read input as string
String consoleInput = input.nextLine();
//if prefix (0x)
if (consoleInput.startsWith("0x")) {
consoleInput = consoleInput.replaceAll("0x", "");
try {
int hex = Integer.parseInt(consoleInput, 16);
System.out.println("Converting from base-16 to base-10.\n" + hex);
} catch (NumberFormatException ex) {
System.out.println("Error converting string: " + consoleInput);
}
} else if (consoleInput.startsWith("0b")) {
consoleInput = consoleInput.replaceAll("0x", "");
try {
int bin = Integer.parseInt(consoleInput, 2);
System.out.println("Converting from base-2 to base-10.\n" + bin);
} catch (NumberFormatException ex) {
System.out.println("Error converting string: " + consoleInput);
}
} else {
System.out.println("I don't know how to covert that number");
}
}
https://stackoverflow.com/questions/28556426
复制相似问题