需要从伪代码编写一个 java 程序,我写了一些代码,它不工作,我不确定我是否做对了,因为我只是试图遵循伪代码 -
- 读我
- 当我 > 0
- 打印余数 i % 2
将 i 设置为 i / 2
import java.util.Scanner; import java.util.Scanner; public class InputLoop { public static void main(String[] args) { int i = 0; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer"); while (!scan.hasNextInt()) // while non-integers are present { scan.next(); System.out.println ("Bad input. Enter an integer."); } while (i>0) // while greater than 0 { int input = scan.nextInt(); System.out.println (i%2); i = (i/2); } } }