我正在尝试在此程序中实现登录功能。我终于弄明白了如何做一个基本的,但遗憾的是我不知道如何结束它,例如如果用户最终达到3的限制它应该结束,但我仍然继续,我不知道我应该放在哪里和什么代码,以便它结束而不是继续主程序。
import java.io。*;
public class Password {
public static void main(String args[]) throws IOException{
String name, un, pw;
String Username = "passwordtest";
String Password = "test123";
int stud;
double math, science, english, filipino, social, ave, sum, fingrade;
BufferedReader inpt = new BufferedReader (new InputStreamReader(System.in));
for(int trial=1; trial<=3; trial++){
System.out.print("Username: ");
un = inpt.readLine();
System.out.print("Password: ");
pw = inpt.readLine();
System.out.println("");
if (un.equals(Username) && pw.equals(Password)){
System.out.println("You have successfully logged in!");
trial=trial+2;
continue;
}else{
System.out.println("Sorry, Incorrect Username/Password");
System.out.println("Please Try Again");
System.out.println("");
}
}
System.out.println("");
System.out.println("Welcome to ITMinions' Grading System!");
System.out.println("How many students' grades would you like to record?");
System.out.print("Answer: ");
stud=Integer.parseInt(inpt.readLine());
System.out.println("");
for (int ctr=1; ctr<=stud; ctr++){
System.out.print("Name of the student: ");
name = inpt.readLine();
System.out.println("");
System.out.println("Input the following grades");
System.out.print("Math: ");
math = Double.parseDouble(inpt.readLine());
if(math<65 || math>100){
System.out.println("");
System.out.println("Wrong input, try again.");
System.out.println("");
ctr=ctr-1;
continue;
}
System.out.print("Science: ");
science = Double.parseDouble(inpt.readLine());
if(science<65 || science>100){
System.out.println("");
System.out.println("Wrong input, try again.");
System.out.println("");
ctr=ctr-1;
continue;
}
System.out.print("English: ");
english = Double.parseDouble(inpt.readLine());
if(english<65 || english>100){
System.out.println("");
System.out.println("Wrong input, try again.");
System.out.println("");
ctr=ctr-1;
continue;
}
System.out.print("Filipino: ");
filipino = Double.parseDouble(inpt.readLine());
if(filipino<65 || filipino>100){
System.out.println("");
System.out.println("Wrong input, try again.");
System.out.println("");
ctr=ctr-1;
continue;
}
System.out.print("History: ");
social = Double.parseDouble(inpt.readLine());
if(social<65 || social>100){
System.out.println("");
System.out.println("Wrong input, try again.");
System.out.println("");
ctr=ctr-1;
continue;
}
sum=math+science+english+filipino+social;
ave=sum/5;
System.out.println("");
System.out.println("The average of " + name + " is: " + ave);
System.out.println("");
}
}
}
请帮忙!是的,这与学校工作有关:) 谢谢!
答案 0 :(得分:0)
我会重写处理成功登录的循环部分,如下所示:
if (un.equals(Username) && pw.equals(Password)){
System.out.println("You have successfully logged in!");
break;
}
请注意使用break关键字来摆脱循环。
当用户使用所有登录尝试时,您可以使用System.exit(0);
退出。
答案 1 :(得分:0)
您必须使用另一个变量,例如:boolean isLoggedIn,并设置成功登录,然后中断而不是继续,如下所示:
if (un.equals(Username) && pw.equals(Password)){
System.out.println("You have successfully logged in!");
isLoggedIn = true;
break;
}else{
System.out.println("Sorry, Incorrect Username/Password");
System.out.println("Please Try Again");
System.out.println("");
}
然后在for循环之外,检查是否(isLoggedIn)并相应地执行操作。
答案 2 :(得分:0)
修改你的fi声明
if (un.equals(Username) && pw.equals(Password)){
System.out.println("You have successfully logged in!");
break;;
}else{
System.out.println("Sorry, Incorrect Username/Password");
System.out.println("Please Try Again");
System.out.println("");
}
}
if(trail==4)
{
//Write your locking logic here
}
在代码中对代码进行硬编码并不是一种好习惯。尝试使用属性文件以简化或如果您有时间使用jdbc
要避免使用Null指针异常
Username.equals(un)
答案 3 :(得分:0)
另外,请确保遵循适当的Java编码标准,例如用于变量命名的camel-case,以及用于常量的所有大写字母。由于用户名和密码是硬编码的,因此它们是非常常见的。所以,改变
String Username = "passwordtest";
String Password = "test123";
到
final String USERNAME = "passwordtest";
final String PASSWORD = "test123";
如果您可以从属性文件加载这些常量也会更好,因为当密码更改时,您无需修改代码,只需编辑属性文件。
答案 4 :(得分:0)
澄清以前的答案:
booelan isLoggedIn = false;
for ( int trials = 3; trials > 0; trials-- )
{
<ask uname, password> // Java convention: don't capitalise variable names
if ( isLoggedIn = <uname/password are OK> {
System.out.println ( "Success" );
break;
}
System.out.printf ( "Bad uname/pass, %d attempts remaining\n", trials );
}
if ( !isLoggedIn ) {
System.out.println ( "User couldn't give valid credentials, quitting after three attempts, due to security reasons" );
Thread.sleep ( 3000 ) // try to fight brute-force attackers
System.exit ( 1 ); // Not zero, it's not a regular end
}
// Go ahead with your application