【发布时间】:2014-04-02 09:57:20
【问题描述】:
所以我是 Java 新手,我很难理解为什么我的程序循环如此之多。
所以我有这个接收文件的程序。它是二进制的,我将其转换为文本。
我猜我离解决方案不是很远,因为最后一个输出基本上是正确的,但我不知道如何得到那个。
public class meu {
public static void main(String[] args)
throws Exception {
File file = new File("sporocilo.txt");
Scanner s = new Scanner(file);
String lastString = "";
while (s.hasNextLine()) {
String line = s.nextLine();
lastString = lastString + line;
StringBuffer result = new StringBuffer();
for (int i = 0; i < lastString.length(); i += 8) {
result.append((char) Integer.parseInt(lastString.substring(i, i + 8), 2));
}
System.out.println(result);
}
}
}
想要的输出:
世界上有10种人:懂二进制的和不懂二进制的。
实际输出:
There ar
There are 10 typ
There are 10 types of pe
There are 10 types of people in
There are 10 types of people in the worl
There are 10 types of people in the world: Those
There are 10 types of people in the world: Those who und
There are 10 types of people in the world: Those who understand
There are 10 types of people in the world: Those who understand binary,
There are 10 types of people in the world: Those who understand binary, and thos
There are 10 types of people in the world: Those who understand binary, and those who do
There are 10 types of people in the world: Those who understand binary, and those who don't.
我们将不胜感激。
【问题讨论】:
-
提示:
System.out.println(result);在循环内。