简体   繁体   English

使用Scanner进行两次检入while循环-Java

[英]Two checks in while loop with Scanner - java

im trying to do two checks with a while loop: 我试图用while循环做两次检查:

1) To show "error" if the user inputs something other than an int 1)如果用户输入的不是int,则显示“错误”

2) Once the user entered an int , if it is one digit, show "two digits only" and keep the loop on until a two digit int has been entered (so an IF should be used as well) 2)一旦用户输入一个int ,如果它是一位数字,则显示“仅两位数字”并保持循环直到输入两位数字int(因此也应使用IF)

Currently I only have the first part done: 目前,我只完成了第一部分:

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter a number");

    while (!scan.hasNextInt()) {

        System.out.println("error");
        scan.next();

    }

However, if possible, I would like to have both checks in one while loop. 然而,如果可能的话,我想有一个 while循环两张支票。

And that's where I'm stuck... 那就是我被困住的地方...

Since you already have two answers. 由于您已经有两个答案。 This seems a cleaner way to do it. 这似乎是一种更清洁的方法。

Scanner scan = new Scanner(System.in);

String number = null;
do {
    //this if statement will only run after the first run.
    //no real need for this if statement though.
    if (number != null) {
        System.out.println("Must be 2 digits");
    }

    System.out.print("Enter a 2 digit number: ");
    number = scan.nextLine();

    //to allow for "00", "01". 
} while (!number.matches("[0-9]{2}")); 
System.out.println("You entered " + number);

First take the input as a String. 首先将输入作为字符串。 If it is convertible to Int then you do your checks, else say 2 digit numbers are acceptable. 如果可以转换为Int,则进行检查,否则说2位数字是可以接受的。 If it is not convertible to a number throw an error. 如果不可转换为数字,则会引发错误。 All this can be done in one while loop. 所有这些都可以在while循环中完成。 And you would like to have a "Do you want to continue? " kind of a prompt and check if the answer is "yes" / "No." 然后,您会收到“是否要继续?”的提示,并检查答案是否为“是” /“否”。 Break from the while loop accordingly. 相应地从while循环中断。

As said above you should always take the input in as string and then try and parse it for an int 如上所述,您应该始终将输入作为字符串输入,然后尝试将其解析为int

package stackManca;

import java.util.Scanner;

public class KarmaKing {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = null;
        int inputNumber = 0;
        while (scan.hasNextLine()) {
            input = scan.next();
            try {
                inputNumber = Integer.parseInt(input);
            } catch (Exception e) {
                System.out.println("Please enter a number");
                continue;
            }
            if (input.length() != 2) {
                System.out.println("Please Enter a 2 digit number");
            } else {
                System.out.println("You entered: " + input);
            }
        }
    }
}

To have it as one loop, it's a bit messier than two loops 要将其作为一个循环,它比两个循环要混乱一些

int i = 0;
while(true)
{
    if(!scan.hasNextInt())
    {
        System.out.println("error");
        scan.next();
        continue;
    }
    i = scan.nextInt();
    if(i < 10 || >= 100)
    {
        System.out.println("two digits only");
        continue;
    }
    break;
}
//do stuff with your two digit number, i

vs with two loops 与两个循环

int i = 0;
boolean firstRun = true;
while(i < 10 || i >= 100)
{
    if(firstRun)
        firstRun = false;
    else
        System.out.println("two digits only");

    while(!scan.hasNextInt())
    {
        System.out.println("error");
        scan.next();
    }

    i = scan.nextInt();
}
//do stuff with your two digit number, i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM