gpt4 book ai didi

java - 如果输入整数以外的任何内容,则要求用户仅输入整数

转载 作者:行者123 更新时间:2023-12-02 04:13:06 25 4
gpt4 key购买 nike

我目前正在使用 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");
}
}
}

有什么帮助吗?谢谢。

最佳答案

像这样使用trycatch()

try{
System.out.print("Player 1 - choose bag: ");
choose = scanner.nextInt();
}catch(InputMismatchException exception)
{
System.out.println("This is not an integer");
}

更新:

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");
}
}
}

关于java - 如果输入整数以外的任何内容,则要求用户仅输入整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com