【发布时间】:2017-09-28 21:50:13
【问题描述】:
我正在编写一个代码,它将二进制数字转换为相应的单词值。
例如,我输入“3”,代码会将数字转换为“11”,即“3”的二进制表示。代码将继续将“11”转换为“one one”,然后输出。
我已经编写了二进制转换部分,但是我很难将其转换为单词。
public class BinaryWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String S = sc.nextLine(); //how many times the for loop will repeat
for (int i = 0; i < S.length() + 1; i++) {
int A = sc.nextInt(); //input the number
String convert = Integer.toBinaryString(A); //converts the number to binary String
String replace = convert.replaceAll("[1 0]", "one, zero "); //replaces the String to its value in words
System.out.println(replace);
}
}
}
我尝试将 replaceAll 函数与正则表达式 [1, 0] 一起使用,它(我认为)会将(两者?)1 和 0 转换为下一个字段中指定的序列。
我想将每个 1 转换为“一”,将每个 0 转换为“零”。
感谢任何帮助,谢谢!
【问题讨论】:
标签: java regex string replace binary