需要用伪代码编写一个java程序,我已经编写了一些代码,它不能工作,我不确定我是否做得对,因为我只是试图遵循伪代码-
发布于 2012-11-22 18:29:08
坦率地说,您的didn't(啊,之前错过了)完全遵循伪代码。伪代码告诉您read i,而您正在读取input。这是个问题。
第二个问题是,您应该在处理输入的while循环之外读取输入。这是你第二次没注意到。
目前您的while循环是:-
while (i>0) // while greater than 0
{
int input = scan.nextInt();
System.out.println (i%2);
i = (i/2);
}这是在您不想要的每次迭代中从用户处读取input。
所以,您需要稍微修改一下代码:-
int i = scan.nextInt(); // Read input outside the while loop
while (i>0) // while greater than 0
{
System.out.println (i%2);
i = i/2; // You don't need a bracket here
}发布于 2012-11-22 18:27:21
不如:
System.out.println(Integer.toBinaryString(i));发布于 2012-11-22 18:27:33
伪代码首先读取(循环外),但在您的代码中读取第二个(循环内部)。
https://stackoverflow.com/questions/13518265
复制相似问题