试图获得仅为int且在1-3之间的输入

问题描述 投票:-3回答:1

我正在制作一个Tic Tac Toe游戏克隆,需要获得有关玩家想要选择的位置的输入。

这是代码:

enter image description here

java java.util.scanner
1个回答
0
投票

hasNextInt()只返回整数。否则检查没有意义。

通过在if语句中调用nextInt(),再次在正文中调用int row = 0; while (scan.hasNext() && row < 1 || row > 3) { String next = scan.next(); try { row = Integer.parseInt(next); } catch (NumberFormatException e) { System.out.println("invalid input " + next); } } ,您将读取两个数字。

尝试

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.