我最近开始进行Java编程
但是我有一个问题 我想写一个程序。我有一个密码,请该程序的用户输入密码 我要:如果此人输入了字符串,我告诉他请不要输入字符串 如果密码是正确的,并且他输入的密码类型(int)是正确的,我告诉他确定。
在程序测试中,我的问题是,当我输入了错误的密码并期望程序告诉我密码不正确时,程序什么也没告诉我!
这是我的代码:
int pass = 123 ;
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
if (password.hasNextInt() && password.nextInt()==pass)
{
System.out.println("ok");
}
else if (password.hasNextInt())
{
System.out.println("wrong pass");
}
else
{
System.out.println("wrong type");
}
答案 0 :(得分:2)
您正在使用Java文档中的hasNextInt()
。
如果此扫描器输入中的下一个标记可以是 解释为int值
因此,您要求两次输入。 例子
Input:
1234 (first Input)
1234 (Then hasNextInt() is asking for input again)
OutPut :
wrong pass
所以我制作了一个简单的代码段供您使用
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
int pass = 123;
try {
int myInput = password.nextInt();
if (myInput == pass) {
System.out.println("ok");
}else{
System.out.println("wrong pass");
}
}catch (java.util.InputMismatchException e) {
System.out.println("wrong type");
}
答案 1 :(得分:0)
尝试以下代码,如果输入的不是整数,则它将抛出NumberFormatException
,并被捕获并显示。
public static void main(String[] args){
int pass = 123 ;
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
String enteredPassword ="";
if(password.hasNext() && (enteredPassword = password.next()) !=null){
try{
if(Integer.parseInt(enteredPassword) == pass){
System.out.println("ok");
}else{
System.out.println("wrong pass");
}
}catch (NumberFormatException nfe){
System.out.println("wrong type");
}
}
}
答案 2 :(得分:0)
您可以使用以下代码。
public static void main(String[] args){
int pass = 123 ;
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
if (password.hasNextInt())
{
if(password.nextInt()==pass) {
System.out.println("ok");
}
else {
System.out.println("Wrong password");
}
}
else
{
System.out.println("wrong type");
}
}
答案 3 :(得分:0)
那一段时间呢?
int MAX_TRIES = 3
int currentTries = 0;
while (password.hasNextInt() && currentTries < MAX_TRIES) {
if (password.nextInt()==pass) {
// OK!
} else {
// Wrong!
}
currentTries++;
}
if (currentTries == MAX_TRIES) {
// You tried too much
} else {
// Password was a string
}
答案 4 :(得分:0)
问题在于,Scanner
之类的nextInt()
方法消耗的输入随后不再可用于以后的Scanner
调用。
int pass = 123 ;
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
if (password.hasNextInt() && password.nextInt()==pass) // line A
{
System.out.println("ok");
}
else if (password.hasNextInt()) // line B
{
System.out.println("wrong pass");
}
else
{
System.out.println("wrong type");
}
因此,如果输入了错误的密码,例如4321,会发生什么?
password.hasNextInt()
作为条件的前半部分。 Scanner
现在不知道该情况,并等待您的控制台输入。您输入4321
,现在Scanner
可以检查该数字是否有效(并且无需消耗4321
就可以使用它,这样它仍然可用)。是的,因此程序继续到条件的下一部分(旁注:如果是abc
,则第一部分将为false,Java将已经知道合并的password.hasNextInt() && password.nextInt()==pass
条件将为false ,而无需进入后半部分,因此不会占用该条目)。password.nextInt()==pass
。这将调用nextInt()
,返回整数4321
,并使用输入。将其与您的数字123
进行比较将得出false,因此条件不匹配。到目前为止,这就是您想要的。123
的情况。但是您的条件password.hasNextInt()
不再看到我们输入的4321
,因为它已在A行中使用。因此它等待下一个输入。这就是问题所在,您在使用了hasNextInt()
的输入之后,仍重新调用nextInt()
。您可以这样更改程序:
int pass = 123 ;
Scanner password = new Scanner(System.in);
System.out.println("Please Enter Your Password : ");
if (password.hasNextInt()) {
if (password.nextInt()==pass) {
System.out.println("ok");
} else {
System.out.println("wrong pass");
}
} else {
pass.next(); // consume the invalid entry
System.out.println("wrong type");
}
[我以更典型的Java风格重新格式化了代码段,虽然当然不会改变功能,但是看起来对我来说更熟悉。 ]
当然,Gatusko的基于异常的方法也能正常工作,我个人将按照他的方式来做,但是也许您现在对异常不满意,所以我尽可能地接近您的方法。 / p>