携程第二道编程,java无法判断输入结束
我真是服了第二题的输入了,没有给定矩阵的N,java的Scanner使用hasNextInt()无法判断输入结束
最后用了定时器强行运行逻辑后退出,通过率83.3%
public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Integer> list = new ArrayList(); Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { int n = (int) Math.sqrt(list.size()); int[][] matrix = new int[n][n]; for (int i = 0; i < list.size(); i++) { matrix[i / n][i % n] = list.get(i); } new Main().new Rotate_Image().rotate(matrix); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } System.exit(0); } }, 2000); while (in.hasNextInt()) { list.add(in.nextInt()); } }