0

我正在尝试在该程序中实现登录功能。我终于弄清楚了如何做一个基本的,但遗憾的是我不知道如何结束它,例如,如果用户最终达到了 3 的限制,它应该结束,但我的仍在继续,我不知道我应该在哪里和什么代码才能结束,而不是继续主程序。

导入java.io.*;

公共类密码{

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("");

 }

}

}

请帮忙!是的,这与学校作业有关:)谢谢!

4

5 回答 5

0

我将重写处理成功登录的循环部分,如下所示:

    if (un.equals(Username) && pw.equals(Password)){
        System.out.println("You have successfully logged in!");
        break;
    }

注意使用 break 关键字来跳出循环。

当用户已使用所有登录尝试时,您可以使用System.exit(0);退出。

于 2013-09-29T10:02:47.057 回答
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 循环之外,检查 if(isLoggedIn) 并相应地执行操作。

于 2013-09-29T10:05:07.363 回答
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

为了避免空指针异常使用

Username.equals(un)
于 2013-09-29T10:08:16.707 回答
0

此外,请确保遵循正确的 Java 编码标准,例如变量命名的驼峰式大小写和常量的全部大写。由于用户名和密码是硬编码的,因此它们实际上是常量。所以,改变

String Username = "passwordtest";
String Password = "test123";

final String USERNAME = "passwordtest";
final String PASSWORD = "test123";

如果您可以从属性文件中加载这些常量也会更好,因为当密码更改时,您无需修改​​代码,只需编辑属性文件。

于 2013-09-29T10:09:57.197 回答
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
于 2013-09-29T10:17:30.687 回答