[英]java : how can i use if correctly
我最近开始进行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");
}
您正在使用Java文档中的hasNextInt()
。
如果此扫描器输入中的下一个标记可以解释为int值,则返回true
因此,您要求两次输入。 例
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");
}
尝试使用此代码,如果输入的不是整数,则将引发NumberFormatException
,该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");
}
}
}
您可以使用以下代码。
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");
}
}
那要怎么办
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
}
问题在于,诸如nextInt()
类的Scanner
方法消耗的输入随后不再可供以后的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
条件为假,无需进入下半部分,因此不会占用该条目)。 password.nextInt()==pass
。 这将调用nextInt()
,返回整数4321
并使用input 。 将其与您的数字123
进行比较将得出false,因此条件不匹配。 到目前为止,这就是您想要的。 123
。 但是您的条件password.hasNextInt()
不再看到我们输入的4321
,因为它已在A行中使用。 因此它等待下一次输入。 这就是问题所在,在使用nextInt()
消耗输入后 ,您仍在调用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的基于异常的方法也能正常工作,我个人会按照他的方式来做,但是也许您现在对异常不满意,所以我尽可能地接近您的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.