当前位置: 首页 > 知识库问答 >
问题:

如何检查System.In是否只包含数字

农英杰
2023-03-14

有一个任务从控制台得到N个数字,找到最长和最短的一个和他们的长度。任务并不难,工作正常,但我决定做一个检查,如果控制台输入与任务的条件相对应:

  • 只有整数。
  • 是精确的N个数字,而不是更多/更少。

我决定编写一个布尔方法isinputCorret(),它将使用扫描器并检查输入是否正确,但它不能正常工作。

public static void main(String[] args) {
    int n = 5;
    Scanner sc = new Scanner(System.in);
    do {
        System.out.println("Hello, please enter " + n + " integer numbers:");
        while (!isInputCorrect(sc,n)){
            System.out.println("Wrong! Try again:");
            sc.next();
        }
    } while (!isInputCorrect(sc, 5));

    String scLine = sc.nextLine();
    String[] arr = scLine.split("\\s+");

    String maxLengthNum = arr[0];
    String minLengthNum = arr[0];
    for (int i = 1; i < arr.length; i++){
        if (maxLengthNum.length() < arr[i].length()){
            maxLengthNum = arr[i];
        }
        if (minLengthNum.length() > arr[i].length()){
            minLengthNum = arr[i];
        }
    }

    String equalMaxNum = "";
    String equalMinNum = "";

    int countMax = 0;
    int countMin = 0;

    for (String s : arr){
        if (maxLengthNum.length() == s.length()){
            countMax += 1;
            equalMaxNum += s + " ";
        }
        if (minLengthNum.length() == s.length()){
            countMin += 1;
            equalMinNum += s + " ";
        }
    }
    if (countMax > 1){
        System.out.println("The longest numbers are: " + equalMaxNum + " Their length is: " + maxLengthNum.length());
    }
    else {
        System.out.println("The longest number is: " + maxLengthNum + " Its length is: " + maxLengthNum.length());
    }
    if (countMin > 1){
        System.out.println("The shortest numbers are: " + equalMinNum + " Their length is: " + minLengthNum.length());
    }
    else {
        System.out.println("The shortest number is: " + minLengthNum + " Its length is: " + minLengthNum.length());
    }
}
public static boolean isInputCorrect(Scanner sc, int n){
    boolean flag = true;
    for (int i = 0; i < n; i++){
        if (sc.hasNextInt()){
            sc.next();
        }else {
            flag = false;
            break;
        }
    }
    return flag;
}

共有1个答案

袁鸿雪
2023-03-14

您可以使用正则表达式来验证输入是否仅包含整数:

int n = 5;
// ... your current code

String scLine = sc.nextLine();
if (!scLine.matches("\\d+(?:\\s+\\d+)*")) {
    throw new IllegalArgumentException("input contained non-integers");
}

String[] arr = scLine.split("\\s+");
if (arr.length != n) {
    throw new IllegalArgumentException("found " + arr.length + " number inputs but expected " + n + ".");
}
 类似资料:
  • 在Java for String类中有一个叫做matches的方法,如何使用这个方法使用正则表达式来检查我的字符串是否只有数字。我尝试了下面的例子,但他们都返回我的结果是假的。

  • 问题内容: 我需要检查字符串是否包含数字。任何数字。字符串是否为数字,而不是数字,但包含一个数字。 例子: ‘test’=没有数字。 ‘test2’=包含数字。 问题答案: 使用正则表达式: 不使用正则表达式:

  • 我有这三个字符串: 我如何检查这些字符串中哪一个只包含字母还是只包含数字(用R表示)? 只能在字母检查中为TRUE 它对很有效,但对也很有效,这是我不想要的。 提前谢了。

  • 假设w、x、y和z都可以在列表a中。是否有一个快捷方式来检查它是否只包含x--例如。而不否定其他变量? w、x、y和z都是单个值(不是列表、元组等)。

  • 问题内容: 在Python中,如何检查是否有数据? 我发现不仅可以检查stdin是否已连接到TTY设备,还可以检查是否有可用数据。 但是如果有人使用诸如 然后使用,它仍然返回True。我需要做什么检查stdin是否有数据? 问题答案: 在Unix系统上,您可以执行以下操作: 在Windows上,虽然select模块只能与套接字一起使用,所以您需要使用其他机制。

  • 问题内容: 我正在实现是否有位图,那么它应该将图像从imageview保存到内部存储器,否则在应用程序的内部存储器中设置另一个位图。这是代码:_ 问题答案: 您可以按以下方式检查它: 只需检查Bitmap值,如下所示: