简体   繁体   English

如何强制用户为int输入固定数量的数字?

[英]How to force user to input a fixed amount of numbers for int?

I want to force the user to input a number with 5 lengths long ( no more and no less) and store them inside an int variable, this includes a leading 0. 我想强迫用户输入5个长度长的数字(不多也不少)并将它们存储在一个int变量内,其中包括一个前导0。

For example, the program should allow the user to input: 例如,该程序应允许用户输入:

12345
04123
00012

But it should not work for: 但这不适用于:

123456
4123
001

I tried... 我试过了...

  if(int x < 99999){
  //continue with code
  }

This would work only if the user input more than 5 lengths but it doesn't resolve the issue of the user putting in an int length less than 5 仅当用户输入的长度大于5时,此方法才有效,但不能解决用户输入的int长度小于5的问题

I think you should take input in string not in int then if the validation goes right you can parse it into integer like the following: 我认为您应该以字符串形式而不是以int形式接受输入,然后如果验证正确,则可以将其解析为整数,如下所示:

import java.util.Scanner;

public class main {

public static void main(String[] args) {
    /// take input
    String userInput = "";
    Scanner sc = new Scanner(System.in);
    userInput = sc.nextLine();
    int input ;
    // validation test
    if(userInput.length() == 5) {
        input = Integer.parseInt(userInput);
    }else {
        // you can display an error message to user telling him that he should enter 5 numbers!
    }
}

}

but you have to know that after parsing it into int if there's a leading zeros it could be gone. 但是您必须知道,如果将其解析为int,并且存在前导零,则它可能会消失。

Something crazy simple like this. 像这样疯狂的简单。 Lacks edge case handling (Read: Negative values) 缺少边缘案例处理(阅读:负值)

boolean matchesLength(int n, int lengthLim){
    char[] charArr = (n + "").toCharArray();
    return (charArr.length == lengthLim);
}

Two functions. 两个功能。 First, to verify the input: 首先,验证输入:

static boolean is_valid_number(String x) {
    // returns true if the input is valid; false otherwise
    if(x.length != 5 || Integer.valueOf(x) > 99999) {  
        // Check that both:
        //    - input is exactly 5 characters long
        //    - input, when converted to an integer, is less than 99999
        // if either of these are not true, return false
        return false;
    }
    // otherwise, return true
    return true;
}

and second, to get user input: 其次,获得用户输入:

static int get_user_input() {
    // Create a scanner to read user input from the console
    Scanner scanner = new Scanner(System.in);
    String num = "";
    do {
        // Continuously ask the user to input a number
        System.out.println("Input a number:");
        num = scanner.next();
        // and continue doing so as long as the number they give isn't valid
    } while (!is_valid_number(num));
    return Integer.valueOf(num);

You might also have to do some error-handling, in case the given input isn't an integer at all. 如果给定的输入根本不是整数,则可能还需要进行一些错误处理。 You could implement is_valid_number() concisely like this: 您可以像这样简洁地实现is_valid_number()

static boolean is_valid_number(String x) {
    try {
        return (x.length == 5 && Integer.valueOf(x) <= 99999);
    } catch (NumberFormatException e) {
        // Integer.valueOf(x) throws this when the string can't be converted to an integer
        return false;
    }
}

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

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