简体   繁体   English

要求用户输入并验证

[英]Ask for user input and verify

I am doing a school JAVA project in which I have ask for user to input a 8 digit string followed by a letter at the end, and I have to verify: 我正在做一个学校JAVA项目,在该项目中,我要求用户输入8位数字的字符串,并在末尾输入字母,并且我必须验证:

  • that I receive those 8 digits and not letters 我收到的是8位数字而不是字母
  • the final letter has to be included in a given char [ ] list. 最后的字母必须包含在给定的char []列表中。

I will paste here the given list and positions 我将在此处粘贴给定的列表和位置

在此处输入图片说明

  • final and most tricky thing - the letter in order to be valid has to be the numbers entered divided by 23 (number of total characters). 最后也是最棘手的事情-为了有效,字母必须是输入的数字除以23(总字符数)。 For example if user inputs 00000102X the only valid letter has to be X. (102%23=10) In case user enters 24659213Q the letter has to be Q (24659213%23=16). 例如,如果用户输入00000102X,则唯一的有效字母必须是X。(102%23 = 10)如果用户输入24659213Q,则字母必须是Q(24659213%23 = 16)。

I'm not so sure where to start..so the only thing I got done till now is: 我不太确定从哪里开始。所以到目前为止,我唯一要做的就是:

    import java.util.Scanner;
public class Practice {

    static final char[] LETTERS = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X'
                                 ,'B', 'N',  'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C'
                                 , 'K', 'E'};
    String [][] input;

    public static void main (String[] args){
    Scanner scanner = new Scanner (System.in);

    System.out.print("Please input: ");

}
}

Any idea how to even start this? 知道怎么开始吗? Any help would be kindly accepted. 任何帮助将被接受。 Thank you! 谢谢!

What I need to do is: 我需要做的是:

  1. ask user to input 8 digits and a letter 要求用户输入8位数字和一个字母
  2. verify composition of the input (8 digits+1 letter) 验证输入的组成(8位数字+ 1个字母)
  3. take digits as a number and divide it by 23. The result should point the accepted letter from the char [ ] list 将数字作为数字并除以23。结果应指向char []列表中可接受的字母
  4. If all OK, save and go back to step 1. If not give error and ask for user to input again. 如果一切正常,请保存并返回至步骤1。如果未出现错误,请要求用户再次输入。

I will give you some design thoughts instead of code so that you can go ahead and write that yourself: 我将给您一些设计思想而不是代码,以便您可以自己编写:

  • think about how to store the user input (eg would it be a list of char s, a String maybe?) - that depends on what you're more comfortable working with but I recommend a String 考虑如何存储用户输入(例如,它是char的列表,也许是String ?)-这取决于您更喜欢使用什么,但是我建议使用String

  • what methods will you need? 您需要什么方法? if you break the task into a number of different checks you'll probably need to 如果您将任务分解为多种不同的检查方式,则可能需要

  • firstly check the length of the String (you can write a method just for this but it'll only be one line of code so I don't recommend this), 首先检查String的长度(您可以为此编写一个方法,但是它仅是一行代码,因此我不建议这样做),

  • then a check that all the characters except the last one are digits and the last one is a String (you could get the substring represented by the first 7 characters and try and parse an Integer out of it, then you could check that the last character is a letter (there are built-in methods in the class Character for checking this and in the String class for working with different parts of the String ) 然后检查除最后​​一个字符以外的所有字符都是数字,最后一个字符是String (您可以获取由前7个字符表示的substring ,并尝试从中parse出一个Integer ,然后可以检查最后一个字符是一个字母(有内置的类中的方法Character检查这一点,并在String类与不同地区工作的String

  • then perhaps a different method that checks that the number represented by the first 7 characters modulo 23 is the index of the last letter in your given char[] array 那么也许是一种不同的方法,该方法检查以23为模的前7个字符表示的数字是否是给定char[]数组中最后一个字母的索引

  • when you have all check methods in place (I think you should simply assert what each method should check inside the body of it eg assert(input.length == 8) or throw a custom or built-in Exception in case the check fails) you can simply chain them up (ie call them one after the other) in your main method 当您具有所有检查方法时(我认为您应该简单地assert每个方法应在其主体中进行检查的内容,例如assert(input.length == 8)或在检查失败的情况下throw自定义或内置的Exception )您可以在主方法中简单地将它们链接起来(即一个接一个地调用它们)

Good luck! 祝好运!

You will need to do this step by step: 您将需要逐步进行此操作:

  1. Check whether string has 9 characters or not. 检查字符串是否包含9个字符。 If not, ask for input again. 如果没有,请再次输入。
  2. Take first 8 characters and use Integer.parseInt(...) over it. 取前8个字符并在其上使用Integer.parseInt(...) If it throws NumberFormatException , ask for input again. 如果抛出NumberFormatException ,则再次要求输入。
  3. Now, you have the number to be validated and the array against which it is to be validated. 现在,您具有要验证的编号以及要对其进行验证的数组。 So, you can take modulus of number and check if the character matches with the modulus index of array. 因此,您可以获取数字模数,并检查字符是否与数组的模数索引匹配。

Example: 例:

class Example{
    final char[] LETTERS = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X'
                                 ,'B', 'N',  'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C'
                                 , 'K', 'E'};

    boolean isLastCharacterValid(int number, char c){  //c means the 9th character, number is the  numeric value of first 8 characters
        return (c == LETTERS[number % LETTERS.length]);
    }
}

A do-while loop would be better for your case. do-while循环更适合您的情况。

You can use : 您可以使用 :

int i = scanner.nextInt();
String str=""+i;
if (str.length() != 8) {
 // ERROR
}
char c = scanner.nextChar();
...

Some examples there: 那里的一些例子:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

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

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