-3

I'm getting an error that

"Illegal start of expression and error ';' expected"

I don't know if I'm doing the new method right, so please help me I need this for school work.

public class Testing{
    public static void main(String args[])throws IOException{

    String un, pw, login;
    final String Username = "javajava";
    final String Password = "testing";

    BufferedReader inpt = new BufferedReader (new InputStreamReader(System.in));

    public void Login(){
    System.out.println("Please enter your Username & Password for you to be able to use the program");
    System.out.println("Warning! Case Sensitive!");

        for(int trial=3; trial>=1; 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("User has successfully logged in!");
                break;
            } else {
                System.out.println("Sorry, you've entered an incorrect Username/Password - (" + (trial-1) + ") retries left");
            }
            if  (trial==1){
                System.out.println("It seems that you've reached the limit for the login attempts, please try again later");
            }
        }
    }
4

3 回答 3

6

您不能[1]在方法中包含方法。

移动

public void Login(){

到外面

public static void main(String args[])throws IOException{.

我建议您阅读教程定义方法


[1]您可以间接地拥有“方法中的方法”。可以有一个包含内部类的方法,并且该类将包含一个方法。所以..你实际上在方法中得到了一个方法;)

于 2013-10-09T12:46:00.543 回答
1

尝试

import java.io.*;

public class Testing {

    static String un, pw, login;
    static final String Username = "javajava";
    static final String Password = "testing";

    public static void Login() throws IOException {
        System.out
                .println("Please enter your Username & Password for you to be able to use the program");
        System.out.println("Warning! Case Sensitive!");
        BufferedReader inpt = new BufferedReader(new InputStreamReader(
                System.in));
        for (int trial = 3; trial >= 1; 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("User has successfully logged in!");
                break;
            } else {
                System.out
                        .println("Sorry, you've entered an incorrect Username/Password - ("
                                + (trial - 1) + ") retries left");
            }
            if (trial == 1) {
                System.out
                        .println("It seems that you've reached the limit for the login attempts, please try again later");
            }
        }
    }
    public static void main(String args[]) throws IOException {
        Login();
    }
}
于 2013-10-09T12:49:10.020 回答
0

您需要在 main 方法之外声明您的方法:

public static void main(String[] args) {...}
public static void Login() {...}
于 2013-10-09T12:51:18.170 回答