[英]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位数字的字符串,并在末尾输入字母,并且我必须验证:
I will paste here the given list and positions 我将在此处粘贴给定的列表和位置
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: 我需要做的是:
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: 您将需要逐步进行此操作:
Integer.parseInt(...)
over it. Integer.parseInt(...)
。 If it throws NumberFormatException
, ask for input again. NumberFormatException
,则再次要求输入。 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.