//Infinite Loop To make the program run continuously.
while (infiniteLoop == 1) {
pickedNumber1 = 1+randNum1.nextInt(12);
pickedNumber2 = 1+randNum2.nextInt(12);
System.out.println("What is " + pickedNumber1 + " multiplied by " + pickedNumber2 + "?");
answer = (pickedNumber1 * pickedNumber2);
Scanner myScanner = new Scanner(System.in);
inputAnswer = myScanner.nextInt();
if (inputAnswer == answer) {
System.out.println("Well done!");
} else {
System.out.println("Please Try Again");
}
}
我想知道如何使用hasNextXXX
方法进行用户验证。
我希望用户只能输入一个整数而不是任何负数或字母字符。
答案 0 :(得分:0)
这应该做的工作:
inputAnswer = myScanner.nextInt();
if(inputAnswer < 0) {
System.out.println("Invalid input, number must be positive");
continue;
}
答案 1 :(得分:0)
尝试这样的事情:
public static void main(String[] args) {
int inputAnswer;
int pickedNumber1;
int pickedNumber2;
int answer;
Random randNum1 = new Random();
while (true) {
pickedNumber1 = 1 + randNum1.nextInt(12);
pickedNumber2 = 1 + randNum1.nextInt(12);
System.out.println("What is " + pickedNumber1 + " multiplied by "
+ pickedNumber2 + "?");
answer = (pickedNumber1 * pickedNumber2);
Scanner myScanner = new Scanner(System.in);
inputAnswer = -1;
while (inputAnswer < 0) {
try
{
System.out.println("Please enter a valid number:");
inputAnswer = myScanner.nextInt();
}catch(Exception e)
{
myScanner.nextLine();
}
}
if (inputAnswer == answer) {
System.out.println("Well done!");
} else {
System.out.println("Please Try Again");
}
}
}
基本循环,直到用户输入大于或等于0的有效数字。每次在用户输入循环之前将inputAnswer重置为-1,以便它最终进入循环。使用try catch块来测试非数字答案。
答案 2 :(得分:0)
你可以用它 首先检查输入是否正确
if (inputAnswer < 0)
{
System.out.println("Wrong input... no. must be greater t hen zero");
}
else if (inputAnswer == answer) // check weather input matches the answer
{
System.out.println("Well done!");
}
else
{
System.out.println("Please Try Again");
}
答案 3 :(得分:0)
您可以执行类似
的操作 Scanner myScanner = new Scanner(System.in);
try {
inputAnswer = myScanner.nextInt();
} catch (Exception e) {
System.out.println("Please try again"); //To check if the input is not character since nextInt() throws exception when character is entered.
continue;
}
if (inputAnswer == answer) { //This will take care of negative numbers since answer will not be negative
System.out.println("Well done!");
} else {
System.out.println("Please Try Again");
}
答案 4 :(得分:0)
我认为这就是你要找的东西:
Scanner input = new Scanner(System.in);
int val;
if(input.hasNextInt() && (val = input.nextInt()) >= 0)
{
//do stuff
}
else
{
//print error message
//clear out the line
input.nextLine();
}
答案 5 :(得分:0)
试试这个...希望帮助你。
while (infiniteLoop == 1) {
int pickedNumber1 = (int) (Math.random() * 12 + 1);
int pickedNumber2 = (int) (Math.random() * 12 + 1);
System.out.println("What is " + pickedNumber1 + " multiplied by "
+ pickedNumber2 + "?");
try{
int answer = (pickedNumber1 * pickedNumber2);
Scanner s1 = new Scanner(System.in);
int inputanswer = s1.nextInt();
if(answer == inputanswer){
System.out.println("Well Done!!");
infiniteLoop=0;
}else{
System.out.println("Wrong Answer Try Again.");
}
}catch(InputMismatchException e){
System.out.println("Not a Number");
}
}