我收到错误
“非法开始表达和错误';'预期“
我不知道我是否正确地使用新方法,所以请帮助我,我需要这个用于学校工作。
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");
}
}
}
答案 0 :(得分:6)
你不能 [1] 在方法中有一个方法。
移动
public void Login(){
到外面
public static void main(String args[])throws IOException{
。
我建议你阅读教程Defining Methods。
[1] 您可以间接地在方法中使用“方法”。可以有一个包含内部类的方法,该类将包含一个方法。所以..你实际上在方法中得到了一个方法;)
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
您需要在主方法之外声明您的方法:
public static void main(String[] args) {...}
public static void Login() {...}