2013-09-29 133 views
0

我正试图在此程序中实现登录功能。我终于想出了如何做一个基本的,但遗憾的是我不知道如何结束它,例如,如果用户终于达到了3的限制,它应该结束,但我仍然继续,我不知道在哪里,为了结束而不是继续执行主程序,我应该放哪些代码。Java基本登录功能

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

} 

} 

}

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

+0

我感谢所有感谢盖世的答案!我终于做到了:) – user2768501

回答

0

我会重写处理succesfull登录如下循环的一部分:使用break关键字

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

通知跳出循环。

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

0

必须使用例如另一个变量:布尔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)并据此采取行动。

0

修改您的网络连接声明

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) 
0

此外,请务必遵循正确的Java编码像驼峰变量的命名标准,所有常量的大写。由于用户名和密码是硬编码的,它们是事实上的常量。因此,改变

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

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

这也将是更好,如果你可以加载这些常量从属性文件,因为当密码更改,您不需要修改代码,只需编辑属性文件。

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