2017-04-12 90 views
0

我正在为一个项目制作一个银行机器代码,并且无论何时登录都会报错。这是做它的代码的部分是这样的:陷入循环?

if(pincheck == pin){ 
       loggedin = true; 
       pincheck = 0; 
       do{ 
        System.out.println("Welcome, " + name); 
        System.out.println(""); 
        System.out.println("Your account balance is $" + balance); 
        System.out.println(""); 
        System.out.println("Press 1 to deposit funds"); 
        System.out.println("Press 2 to withdraw funds"); 
        System.out.println("Press 3 to log out"); 
        System.out.println(""); 
        options = in.nextInt(); 

        switch (options) { 
         case 1: System.out.println("How much would you like to deposit?");   // deposit 
           deposit = in.nextFloat(); 

           balance = balance + deposit; 
           deposit = 0; 

           System.out.println("You have deposited funds into your account."); // withdraw 
           System.out.println(""); 
          break; 
         case 2: System.out.println("How much would you like to withdraw?"); 
           withdraw = in.nextFloat(); 

           balance = balance - withdraw; 
           withdraw = 0; 

           System.out.println("You have removed funds from your account."); 
           System.out.println(""); 
          break; 
         case 3: System.out.println("Logging out...");        // log out 
           System.out.println(""); 
           loggedin = false; 
          break; 
         default:System.out.println("Please enter a valid number");     // Invalid number 
          break; 
        } 
       }while(loggedin = true); 

正在发生的事情是登录在你需要把一个号码,pincheck,如果它等于存在,它会记录该引脚你可以登录,但是当我按3登出时,它会打印登出并打印欢迎信息,整个事情再次开始。任何人都可以指出我卡住的地方吗?

+2

你知道赋值'='和比较等号'=='之间的区别? –

+0

我是一个相对较新的人,如果我只是搜索了一下我认为会发生的事情,它会告诉我我已经知道的事情。感谢您的帮助,我修复了代码,它现在正在工作! –

回答

2

=是一个赋值操作符,所以你只是分配值(即,设置loggedin=true),其总是会true(因为你设置为true)。

那么,你是不是检查循环中的实际情况,所以你需要更正while条件如下图所示,它使用==运算符(用于评估的条件表达式):

while(loggedin == true); //use == for condition evaluation