import java.util.Scanner;//daobao
public class Login2{
public static void main(String[]args){
int random=(int)(Math.random()*9000)+1000;
Scanner input=new Scanner(System.in);// create Scanner object
System.out.println("------Welcome to the login system------");
System.out.println("Please enter the name of the user:");
String userName=input.next(); //get the username
System.out.println("Please enter the private code:");
String pwd=input.next();//get the private code
System.out.println("Please enter the vertifitcation code:");
String vertificationCode=input.next();//get the vertification code
int number=input.nextInt();
//using the more if conditions to judge the username,passwor and vertification code
if(!"Wangwu".equals(userName)){
System.out.println("I am sorry, the user is not exit");
}else if(!"Wang123".equals(pwd)){
System.out.println("Sorry, the password is not right");
}else if(number!=random){
System.out.println("Sorry,the vertification code is wrong");
}else{
System.out.println("Login succesfully");
}
}
}
我想生成的是,输错用户名就立即报错,但是我在运行这段代码后,输入错误的用户名,它还是允许我继续输入密码,而不是报错。
你应该把输入密码的代码放到判断用户民正确的if语句里面
代码位置问题,你把第二个next的获取密码的放到用户名成功的里面
计算机在执行你的代码的时候,是逐行执行的,你代码的逻辑顺序也是计算机的计算顺序。
直接把用户名和密码的判断用&&连起来不就能解决了吗?