我目前正在用Java编程创建“Nim”游戏。但是,我完全迷失了如何修改我的程序以使用户只输入整数,如果他们输入任何其他内容,请他们输入一个整数。
import java.util.Scanner;
public class Nim
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int noEntered;
int firstPile = 3;
int secondPile = 4;
int thirdPile = 5;
int next = 1;
int choose;
do
{
if (next % 2 == 1)
{
System.out.print("Player 1 - choose bag: ");
choose = scanner.nextInt();
if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 2)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 3)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
}
else
{
System.out.print("Player 2 - choose bag: ");
choose = scanner.nextInt();
if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 2)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
}
next++;
}
while (firstPile != 0 && secondPile != 0 && thirdPile != 0);
if (next % 2 == 1)
{
System.out.println("Game Over - Player 1 wins");
}
else if (next % 2 == 0)
{
System.out.println("Game Over - Player 2 wins");
}
}
}
有任何帮助吗?感谢。
答案 0 :(得分:1)
像这样使用try
和catch()
,
try{
System.out.print("Player 1 - choose bag: ");
choose = scanner.nextInt();
}catch(InputMismatchException exception)
{
System.out.println("This is not an integer");
}
UPDATE:
import java.util.Scanner;
public class Nim
{
public static void main(String[] args)
{
try{
Scanner scanner = new Scanner(System.in);
int noEntered;
int firstPile = 3;
int secondPile = 4;
int thirdPile = 5;
int next = 1;
int choose;
do
{
if (next % 2 == 1)
{
System.out.print("Player 1 - choose bag: ");
choose = scanner.nextInt();
if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 2)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 3)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
}
else
{
System.out.print("Player 2 - choose bag: ");
choose = scanner.nextInt();
if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 2)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
else if (choose == 1)
{
System.out.print("Now choose no. of tokens: ");
noEntered = scanner.nextInt();
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
}
next++;
}
while (firstPile != 0 && secondPile != 0 && thirdPile != 0);
if (next % 2 == 1)
{
System.out.println("Game Over - Player 1 wins");
}
else if (next % 2 == 0)
{
System.out.println("Game Over - Player 2 wins");
}
}catch(InputMismatchException exception){
System.out.println("Given input is not a number");
}
}
}
答案 1 :(得分:1)
在你的问题模式中,匹配对于避免使用try catch块进行异常捕获和处理非常有用,你可以定义一个模式来识别整数。
static Pattern PATTERN = Pattern.compile( "^(-?0|-?[1-9]\\d*)" );
验证整数。
public static boolean isInteger( String value ) {
return value != null && PATTERN.matcher( value ).matches();
}
使用方法获取输入并验证输入是否仅为整数否则显示消息,如"请输入有效的整数"并等待下一个有效输入。
public static Integer getIntInput(Scanner scanner){
String sentanse=scanner.next();
if(isInteger(sentanse)){
System.out.println("number "+sentanse);
return Integer.parseInt(sentanse);
}else{
System.out.println("Please enter valid Integer");
return getIntInput(scanner);
}
}
要查看结果,请修改如下代码
public class Nim {
static Pattern PATTERN = Pattern.compile("^(-?0|-?[1-9]\\d*)");
public static boolean isInteger(String value) {
return value != null && PATTERN.matcher(value).matches();
}
public static Integer getIntInput(Scanner scanner) {
String sentanse = scanner.next();
if (isInteger(sentanse)) {
System.out.println("number " + sentanse);
return Integer.parseInt(sentanse);
} else {
System.out.println("Please enter valid Integer");
return getIntInput(scanner);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int noEntered;
int firstPile = 3;
int secondPile = 4;
int thirdPile = 5;
int next = 1;
int choose;
do {
if (next % 2 == 1) {
System.out.print("Player 1 - choose bag: ");
choose = getIntInput(scanner);
if (choose == 1) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + +firstPile + ", " + secondPile + ", "
+ thirdPile);
} else if (choose == 2) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
} else if (choose == 3) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
} else {
System.out.print("Player 2 - choose bag: ");
choose = getIntInput(scanner);
if (choose == 1) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
firstPile = firstPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
} else if (choose == 2) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
secondPile = secondPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
} else if (choose == 1) {
System.out.print("Now choose no. of tokens: ");
noEntered = getIntInput(scanner);
thirdPile = thirdPile - noEntered;
System.out.println("Bag Status: " + firstPile + ", " + secondPile + ", "
+ thirdPile);
}
}
next++;
} while (firstPile != 0 && secondPile != 0 && thirdPile != 0);
if (next % 2 == 1) {
System.out.println("Game Over - Player 1 wins");
} else if (next % 2 == 0) {
System.out.println("Game Over - Player 2 wins");
}
}
希望这可以帮助您解决问题。
答案 2 :(得分:0)
你可以使用 scanner.hasNextInt() 找出是否有int
答案 3 :(得分:0)
public static int validateNumber () {
try {
return scanner.nextInt();
} catch (InputMismatchException e ) {
scanner.next();
System.out.println("not a number");
return 0 ;
}
}
答案 4 :(得分:0)
if(scanner.hasNextInt())
这里参考: [http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()][1]