反正我可以停止循环循环吗

时间:2019-01-06 14:04:03

标签: java if-statement while-loop

当我尝试插入正确的密码1234“ Access Granted”时。 但是,当我尝试多次尝试插入它时,它将输出“ Access Denied”,但不应输出。

import java.util.*;

class access {
    void granted () {
        System.out.println("Access Granted!"); 
    }

    void denied() {
        System.out.println("Access Denied!");
        System.out.println("Please try again! Re-enter your passcode: ");
    }

    void ct1 () {
        System.out.println("System is disabling now due to multiple attempts made");
    }
}

class passEnter extends Passcode {
    void Enter1 () {
        Scanner sc = new Scanner(System.in);
        super.pass = sc.nextInt();
    }
}

public class Passcode {

    static int code = 1234;
    static int pass;

    public static void main (String [] agrs) {

        access access = new access();
        passEnter reEnter = new passEnter();
        int ct = 0;

        System.out.println("Enter your passcode ");
        reEnter.Enter1();

        // This is where my problem begins...
        if (pass != code) {
            while (ct < 2) {
                ct++;
                access.denied();
                reEnter.Enter1();
            } 
            access.ct1();
        } 
        else {
            access.granted();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果一次输入错误的密码,则进入while循环。您要求在此循环内输入下一个条目。因此,循环将重复进行。这就是为什么一旦输入了错误的密码后就永远无法输入您所说的已授予的else块的原因。