2017-04-12 2 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

=는 할당 연산자, 그래서 당신은 단순히 항상 true 될 것이다 (즉, loggedin=true 설정) 값을 할당하는 (당신은 true로 설정하기 때문에).

그래서, 당신은 루프의 실제 상태를 확인하지 않는, 그래서 (조건식을 평가하기 위해 사용) == 연산자를 사용하는 아래와 같이 당신은 while 조건을 정정해야합니다 당신은 알고

while(loggedin == true); //use == for condition evaluation 
관련 문제