简体   繁体   English

如何获取用户输入的int数并同时存储用户的输入?

[英]how to take the number of an int input from the user and at the same time store the user's input?

this program is a magic date program (day times month="year as a two digits number") like 06\\10\\60 ... I want to make a condition ... if the day or the month or the year entered are more or less than 2 digits should display an error message ... I tried this 该程序是一个神奇的日期程序(日期时间month =“ year作为两位数的数字”),例如06 \\ 10 \\ 60 ...我想做一个条件...如果输入的日期或月份或年份是大于或小于2位数应显示一条错误消息...我尝试过

 System.out.println("Please enter the day as a two digit Number");
 day=key.nextInt();
 day1=day.length();

but it did not work ... so I tried this : 但是它没有用...所以我尝试了这个:

System.out.println("Please enter the month as a two digit Number");
month1=key.next().length();
month=key.nextInt();

but when i run the program ... it requires two inputs not one So help me out 但是当我运行程序时...它需要两个输入而不是一个所以请帮帮我

Simply: 只是:

day = key.nextInt();
int dayAbs = Math.abs(day);      // to handle negatives
if(dayAbs >= 10 && dayAbs <= 99) {
    // ...
}

The problem is that you are using nextInt() but you would like two-digit numbers like 06 which is not a valid integer. 问题是您使用的是nextInt()但是您想要两位数字,例如06 ,这不是有效的整数。

It will be better to: 最好:

  1. Scan the input as a String. 将输入扫描为字符串。 String day = scan.nextLine();

  2. Validate for correct input by using day.matches("\\\\d{2}") 使用day.matches("\\\\d{2}")验证输入是否正确
    (This will only match any pairing of [0-9] and don't have to worry about other characters) (这将仅匹配[0-9]的任何配对,而不必担心其他字符)

  3. If it Validates then you can get your Integer: Integer realDay = Integer.valueOf(day); 如果它验证,则可以获取Integer: Integer realDay = Integer.valueOf(day);

  4. Perform further Validation to test the range (1-31 for Days) (1-12 for Months) (1-28 or 1-29 for February depending on the Year) etc. 进行进一步验证以测试范围(天数为1-31)(月数为1-12)(2月为1-28或1-29,具体取决于年份)等。

You can do this simply by converting int to string and then by checking the length of the string. 您可以简单地通过将int转换为字符串,然后检查字符串的长度来做到这一点。

String s = Integer.toString(day);
if(s.length() <= 2 && !(s.length() < 1)){
    System.out.println("Good");
}else
    System.out.println("Bad"); 

to test for the number of digits in an int primitive try this 测试int原语中的位数

int number = ...;
// or however you get the number

int length = (int)(Math.log10(number)+1);

if(length!=2) {
    // no!
} else {
    // yuss!
}

Except using int use string then check the length of the input. 除了使用int使用字符串,然后检查输入的长度。 This code is only a base. 此代码仅是基础。 It is stored in result. 它存储在结果中。 Then after the check use Integer.parseInt(result); 然后在检查后使用Integer.parseInt(result);

System.out.println("Please enter two digits:");
    Scanner scan = new Scanner(System.in);
    String result = scan.next();
     if(result.length()>2 || result.length()<2)
         System.out.println("ERROR");
     else
         System.out.println("Success");

Actually, it seems that you want to enforce the length of the string so that the user has to type 06 instead of 6 . 实际上,似乎您想强制执行字符串的长度,以便用户必须键入06而不是6

If so: 如果是这样的话:

String dayString = key.next();
if(dayString.length() != 2) {
    // throw error
}
int day = Integer.parseInt(dayString);
int day=key.nextInt();
String dayString = "";
if(day> 30 || day <0){ System.out.println("Error1");}
else if(day<10 ){ dayString = "0" + String.valueOf(day);}
else{dayString = String.valueOf(day);}

here u have ur day in 2 digits as a string,more easy to manipulate. 在这里,您的一天以2位数字作为字符串,更易于操作。 u can also try Date class (java.util.Date) 您还可以尝试Date类(java.util.Date)

i don't understand that why you are using key but i think that Buffer reader can solve your problem here is the example! 我不明白为什么您要使用密钥,但是我认为Buffer reader可以解决您的问题,这里就是示例!

int month=0,length=0;
        String len=null;
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the month as a two digit Number");
        len=bf.readLine();
        length=len.length();
        month=Integer.parseInt(len);
        if(length==2)
        {
            System.out.println("Here you go month is two digit!");
        }
        else
        {
           //inform user that month is not 2 digits!
        }

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

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