如何在程序终止之前让登录系统进行三次尝试。 [英] How do I get a login System to have three attempts before the program terminates.
本文介绍了如何在程序终止之前让登录系统进行三次尝试。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< pre lang =java>
import java.io. *;
import java.util。*;
公共类C1Task4 {
public static void main(String [] args)throws FileNotFoundException {
Scanner console = new Scanner(System.in );
String NameInput;
String PassInput;
String NumberInput;
int count = 0;
System.out.println(请输入账号);
NumberInput = console.next();
System.out.println(请输入密码);
PassInput = console.next();
for(count = 0; count< 3; count ++){
if(PassInput.equals(Password)&&&(NumberInput。 equals(123321)))
{
System.out.println(你已登录!);
}
其他
{
System.out.println(你还有两次尝试登录);
}
}
}
}
解决方案
尝试这样的事情:
for (count = 2 ; count> 0 ; --count){
if (PassInput.equals( 密码)&&(NumberInput.equals( 123321)))
{
System.out.println(< span class =code-string> 您已登录!);
count = 1 ; // 显示成功
break ; // 成功,不再需要尝试
}
else
{
// 检查失败,如果两个失败已经失败了循环
如果(count == 0 )
break ;
// 还允许更多尝试
System.out.println( 你有 + count + 更多尝试登录);
}
}
如果(count == 0 )
{
// 对登录失败采取措施
}
<pre lang="java"> import java.io.*; import java.util.*; public class C1Task4 { public static void main(String[] args)throws FileNotFoundException { Scanner console = new Scanner(System.in); String NameInput; String PassInput; String NumberInput; int count = 0; System.out.println("Please enter Account Number"); NumberInput = console.next(); System.out.println("Please enter Password"); PassInput = console.next(); for(count=0; count<3; count++){ if (PassInput.equals("Password")&& (NumberInput.equals("123321"))) { System.out.println("You have logged in!"); } else { System.out.println("You have two more attempts to log in"); } } } }
解决方案
Try something like this:
for(count = 2; count > 0; --count) { if (PassInput.equals("Password")&& (NumberInput.equals("123321"))) { System.out.println("You have logged in!"); count = 1; // show success break; // success, no more tries needed } else { // check failed so if two fails already then break out of loop if (count == 0) break; // still more tries allowed System.out.println("You have " + count + " more attempts to log in"); } } if (count == 0) { // take action for login failure }
这篇关于如何在程序终止之前让登录系统进行三次尝试。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文